How to get local server host and port in Spring Boot?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting the local server port in Spring Boot is straightforward once the embedded web server has started. Getting the host is more nuanced, because the server may bind to all interfaces, in which case there is no single meaningful host name unless you choose one for your own local URL generation.
Understand configured values versus runtime values
Spring Boot exposes configuration such as server.port, but that is not always the same as the actual runtime port. If you use server.port=0, the framework asks the operating system for a free port, so you must read the value after startup from the running web server.
For the host, server.address may be unset. When that happens, the application is typically bound to all available interfaces rather than one specific local host string.
Read the runtime port from the web server
A reliable pattern is to listen for WebServerInitializedEvent and store the assigned port.
This is a good general solution for application code that needs to log or publish its own local base URL.
Use @LocalServerPort in tests
If the real use case is integration testing, Spring Boot already provides a simpler option.
This is the right choice in tests because it avoids manual event handling and integrates cleanly with random-port startup.
Host is a design decision, not always a discoverable fact
Many questions about host and port really mean, "How do I build a URL for my app?" Port is usually discoverable. Host often is not, because a server bound to 0.0.0.0 can be reached through several addresses. In that situation you normally choose a local loopback host such as 127.0.0.1 for local URLs or use a configured public host name when running behind a proxy.
That distinction matters. If you present one host value as if it were the only true answer, you can create misleading links in containerized or cloud setups.
Servlet versus reactive applications
The overall idea is the same for servlet and reactive stacks: wait until the web server exists, then read the assigned port from the running server context. What changes is the surrounding application type, not the need to distinguish configured properties from the actual runtime listener.
Common Pitfalls
- Reading
server.porttoo early and expecting it to reflect the assigned random port. - Treating
server.addressas mandatory when it is often unset. - Assuming there is always one correct host string even when the server listens on all interfaces.
- Using
@LocalServerPortin normal application code instead of tests. - Building absolute URLs without considering proxies, forwarded headers, or deployment-specific host names.
Summary
- Read the runtime port after startup if the server chooses it dynamically.
- '
WebServerInitializedEventis a reliable application-level hook for that.' - Use
@LocalServerPortwhen the need is test-specific. - Host discovery is contextual because a server can bind to multiple interfaces.
- For local URLs, choose an explicit host strategy instead of assuming Spring Boot always has one canonical answer.
Related reading
- How to get milliseconds from LocalDateTime in Java 8
- How to get parameters from the URL with JSP
- How to get request URL in Spring Boot RestController
- How to get rid of Incremental annotation processing requested warning?
- How to get Spinner value?
- How to get Spring RabbitMQ to create a new Queue?
- How to get status code from webclient?
- How to get the concrete class name as a string?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.