Home » Change the port in Spring boot

Change the port in Spring boot

1. Overview

In this article, we will learn to change the default port of the embedded server in a Spring boot application. By default, the embedded server listens for HTTP requests on port 8080. To learn more about Spring, refer to our articles.

2. Change the port in Spring boot

We will explain all the ways to specify the embedded server port such as properties file, programmatically, VM arguments, command-line arguments.

If you want to scan and use any free port available, then mention the server port as 0. For example, server.port=0.This helps to prevent clashes with other ports that are already in use.

2.1. Configure application properties

You can configure the port by defining the properties in your application.properties or application.yaml file:

server.port=8081
server:
    port: 8081

Usually, we place the file under src/main/resources for Maven or Gradle projects.

2.2. Change port in VM options \ System property

Alternatively, you can specify the port using the VM argument -Dserver.port. In your IDE, navigate to Run -> Edit or Run Configurations -> VM options

-Dserver.port=8090
Change port in STS or Eclipse
STS or Eclipse
change default port in IntelliJ
IntelliJ

If you are running the jar file from the command prompt instead of IDE, then pass the server port property in the command.

java -jar -Dserver.port=8081 <path to jar file>

You can also programmatically set the System server port property.

public static void main(String[] args) {
   System.setProperty("server.port", "8081");
   SpringApplication.run(HibernateApplication.class, args);
}

2.3. Pass the port in command line \ program argument

By default, SpringApplication converts any command-line option arguments to a property and adds them to the Spring Environment. If you are running the jar from the terminal, then add the command line argument in the command:

java -jar <path to Jar file> --server.port=8081 
–server.port program argument passed in args to the main method

Note that --server.port is a command-line or program argument that is passed in the args parameter of the SpringBoot application’s main method, whereas --Dserver.port is a JVM system property.

You can also set the program arguments in your IDE by navigating to Run -> Edit \ Run configurations -> Program arguments.

Assume you want to keep the name of your command-line arguments short. For example, --pt instead of --server.port.

To achieve this behavior, you can use placeholders in properties or yml file. It allows you to use  --pt=8081 instead of --server.port=8081.

java -jar <path to Jar file> --pt=8081

You can use placeholder ${pt:8080} in your properties or yml file. Here, 8080 is the default value to use when the command line argument pt is not specified.

server.port=${pt:8080}
server:
  port: "${pt:8080}"

2.4. Using WebServerFactoryCustomizer from Spring 2.x and above

If you need to configure your embedded servlet container programmatically, then you can register a Spring bean that implements the WebServerFactoryCustomizer interface. 

The beans of this type get a callback before the server start, so you can set the port.

This interface provides access to the ConfigurableServletWebServerFactory through which you can set the port.

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(8081);
    }

}

2.5. Using EmbeddedServletContainerCustomizer in Spring 1.x

The EmbeddedServletContainerCustomizer class is replaced by WebServerFactoryCustomizer in the Spring 2.x.x and above versions. So, you can use this only in Spring 1.x versions. The beans of type EmbeddedServletContainerCustomizer get a callback before the server start, so you can set the port.

@Component
public class CustomizationBean implements  EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) 
    {    
        container.setPort(8081);
    }
}

(or)

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return (container -> {
        container.setPort(8081);
    });
}

2.6. Change port using the environment variable

The SERVER_PORT OS Environment variable is available to define the port:

UNIX Shell

> SERVER_PORT=7788 
> java -jar <jar file>

Windows

> SET SERVER_PORT=7788 
> java -jar <jar file>

3. Conclusion

The priority order goes as below:

1. Programmatic configuration
2. Command Line Arguments
3. System property and property files

To sum up, we have learned to change the default port of the embedded server in your Spring boot application.

Leave a Reply

Your email address will not be published.