Spring Boot
host address
port address
runtime configuration
Java programming

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.

Browse interview questions

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.

java
1import org.springframework.boot.web.context.WebServerApplicationContext;
2import org.springframework.boot.context.event.ApplicationReadyEvent;
3import org.springframework.context.event.EventListener;
4import org.springframework.stereotype.Component;
5
6@Component
7public class ServerInfoLogger {
8
9    private final WebServerApplicationContext context;
10
11    public ServerInfoLogger(WebServerApplicationContext context) {
12        this.context = context;
13    }
14
15    @EventListener(ApplicationReadyEvent.class)
16    public void logAddress() {
17        int port = context.getWebServer().getPort();
18        System.out.println("Server is listening on port " + port);
19    }
20}

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.

java
1import org.springframework.core.env.Environment;
2import org.springframework.stereotype.Component;
3
4@Component
5public class AddressInfo {
6
7    private final Environment environment;
8
9    public AddressInfo(Environment environment) {
10        this.environment = environment;
11    }
12
13    public String getConfiguredAddress() {
14        return environment.getProperty("server.address", "0.0.0.0 or default interface binding");
15    }
16}

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

java
1import org.springframework.boot.web.context.WebServerApplicationContext;
2import org.springframework.core.env.Environment;
3import org.springframework.stereotype.Component;
4
5@Component
6public class RuntimeAddressService {
7
8    private final WebServerApplicationContext context;
9    private final Environment environment;
10
11    public RuntimeAddressService(WebServerApplicationContext context, Environment environment) {
12        this.context = context;
13        this.environment = environment;
14    }
15
16    public String describe() {
17        String host = environment.getProperty("server.address", "localhost");
18        int port = context.getWebServer().getPort();
19        return host + ":" + port;
20    }
21}

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 WebServerApplicationContext after startup to get the actual runtime port.
  • Read server.address from 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions