Discover Your Running Port in Spring Boot

Spring Boot – How to get the running port

Introduction:

Spring Boot is a popular framework for building Java applications with minimal configuration requirements.

One common requirement in Spring Boot applications is to retrieve the running port on which the application is currently running.

Overview:

When a Spring Boot application starts, it typically listens on a specific port number for incoming HTTP requests.

This port number is dynamically assigned unless explicitly specified in the application’s configuration.To retrieve the running port programmatically, we need to access the application’s environment.

Problem:

The problem is that when running a Spring Boot application, we might want to know the port number it’s using to interact with the application.

Solution:

To get the running port in a Spring Boot application, we can use the following code snippet:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.stereotype.Component;

@Component
public class PortRetriever {

  @LocalServerPort
  private int port;

  public int getRunningPort() {
    return port;
  }
}

In the above code, we use the `@LocalServerPort` annotation to automatically inject the running port into the `port` field.

By creating a component or bean with this code, we can then retrieve the running port using the `getRunningPort` method.

Note that the `PortRetriever` class needs to be a Spring-managed component (annotated with `@Component`, `@Service`, etc.) for the injection to work.

Key points to address:

– Spring Boot provides a few different ways to obtain the running port.

– The most common approach is to use the `ServerPort` annotation.- Another way is to use the `ServerProperties` class.

– Additionally, you can also access the running port programmatically using the `Environment` object.

Explain the Core Concept:

In Spring Boot, you can easily retrieve the running port of your application by using the `Environment` object.

The `Environment` interface provides various methods to access the application’s configuration properties, including the server port.

To get the running port of your Spring Boot application, follow these steps:

1. Autowire the `Environment` object in your class:


@Autowired
private Environment environment;

2. Use the `getProperty()` method of the `Environment` object to retrieve the value of the `local.server.port` property:


int port = Integer.parseInt(environment.getProperty("local.server.port"));

The `local.server.port` property holds the value of the running server port.

This property is automatically set by Spring Boot when the application starts.

Now you can use the `port` variable to perform any necessary operations or logging related to the running port of your Spring Boot application.

Different Solutions with code samples:

1. Using the `ServerProperties` class:


@Autowired
private ServerProperties serverProperties;

// ... 

int port = serverProperties.getPort(); System.out.println("Running on port: " + port);

2. Using the `Environment` class:


@Autowired
private Environment environment;

// ... 

int port = Integer.parseInt(environment.getProperty("local.server.port")); System.out.println("Running on port: " + port);

3. Using the `ServletWebServerApplicationContext` class:


@Autowired
private ServletWebServerApplicationContext webServerAppCtxt;

// ... 

int port = webServerAppCtxt.getWebServer().getPort(); System.out.println("Running on port: " + port);

Conclusion:

These solutions provide different methods to obtain the running port in a Spring Boot application.

You can use any of these approaches depending on your specific requirements and preferences.

Leave a Comment

Your email address will not be published. Required fields are marked *