How to get the SPRING Boot HOST and PORT address during run time?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting the runtime port from Spring Boot is straightforward. Getting the "host address" is more nuanced. Spring Boot knows which port the embedded server actually bound to, but the externally reachable host name is often an infrastructure concern rather than something the application can reliably infer on its own.
Getting the Actual Port
If your app starts an embedded web server, the cleanest place to read the real port is after the server is ready.
This works well even when you configured server.port=0 and asked Boot to pick a random free port.
Getting the Configured Address
If you explicitly set the bind address with server.address, you can read that from the Environment.
That gives you the configured bind address, not necessarily the public URL users should type into a browser.
Why the Host Question Is Harder
Port is local server state. Host is often not.
For example, the app may be:
- behind a reverse proxy
- running in Kubernetes behind a Service or Ingress
- behind a load balancer
- bound to
0.0.0.0 - exposed through a different public DNS name than the local machine name
In all of those cases, there is no single universally correct "application host address" that Spring Boot can magically report. The app can know what interface it bound to, but not always what external host clients use.
That is why many teams distinguish between:
- bind address and port
- public base URL
The public base URL is often configured separately, for example as an application property such as app.public-base-url.
A Practical Combined Example
This is reasonable for local diagnostics, but do not confuse it with a guaranteed externally reachable address in cloud or container deployments.
Special Case: Tests
In integration tests, Spring Boot provides dedicated helpers. For example, when using a random port in web tests, @LocalServerPort is often the most direct choice.
That is a testing convenience, not the usual production runtime pattern.
Common Pitfalls
The most common mistake is assuming the machine's local network address is the same as the public host that users or proxies reach.
Another mistake is reading only server.port from configuration and assuming that is always the real runtime port, even when the server may have been assigned a random port.
A third pitfall is logging 0.0.0.0 as if it were a client-usable address. It is a bind target, not a browser URL.
Summary
- Use
WebServerApplicationContextafter startup to get the actual runtime port. - Read
server.addressfrom the environment only if you want the configured bind address. - The public host name is often infrastructure-defined and should usually be configured explicitly.
- Do not assume the local bind address equals the externally reachable URL.
- Treat host and port as different kinds of runtime information.
Related reading
- How to get the ThreadPoolExecutor to increase threads to max before queueing?
- How to get the type of T from a member of a generic class or method
- How to get the unique ID of an object which overrides hashCode()?
- How to get the user input in Java?
- How to get thread id from a thread pool?
- How to get topic list from kafka server in Java
- How to get UTF-8 working in Java webapps?
- How to get UTF-8 working in Java webapps?

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.