Change Default Port in Spring Boot Application

This quick tutorial covers various ways to Change the default port of a Spring Boot Application with examples.

Overview

Spring Boot comes with an embedded tomcat server. The tomcat in Spring boot is configured to run on a default port of 8080. When we start as a Spring Boot Application without any port configuration, the log file clearly denotes that it runs on the default port.

o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
com.amitph.spring.dogs.Application       : Started Application in 3.973 seconds (JVM running for 4.33)
Code language: plaintext (plaintext)

Spring Boot also offers ways to configure and change the port number that the application listens on. We will begin with changing the port number by using properties configurations files. Next, we will see how can we assign a random port to launch a Spring Boot application. We will also see, how can we pass the desired port number as a command line argument while launching the application. Finally, we will have a look at a programmatic approach to change the port number.

Using Properties Configuration

Spring Properties files are kept under the src/main/resources directory and they represent various application level configurations. The tomcat port is represented in the form of server.port property.

To use a different port number of 9191, we can add the server.port property in the applications properties file.

server.port=9191Code language: Properties (properties)

If your application is using application yaml files instead, the port number can be configured as shown here.

server:
  port: 9191Code language: YAML (yaml)

Now, when we launch the application, we can see the port number is changed.

o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9191 (http) with context path ''
com.amitph.spring.dogs.Application       : Started Application in 3.787 seconds (JVM running for 4.119)Code language: plaintext (plaintext)

This way is the easiest of all other ways of changing the application port number. Also, we can use Spring Boot Environment Profiles and and create different properties configuration for different environments. This allows us to have a different port in different environments.

Assign Random Port

When your application doesn’t need to be run on a specific port, we can let Tomcat decide to chose any valid and free port on the device. We can do this by setting the port number to 0 (zero).

server:
  port: 0Code language: YAML (yaml)

When we do that and launch the application, it will start on any random port number.

o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 57469 (http) with context path ''
com.amitph.spring.dogs.Application       : Started Application in 3.496 seconds (JVM running for 3.833)Code language: plaintext (plaintext)

However, it is important to keep in mind that, the port number will change every time the application is started and restarted.

Command Line Argument

We can package a Spring Boot application as a JAR file and launch it from command line. While doing so, we can pass several important configurations as command line arguments and server port number is one of them.

java -jar -Dserver.port=9191 dog-service.jarCode language: Bash (bash)

Even if the properties configuration is used to configure port number, the command line argument takes preference. Using this option we can configure the port number at the time of the deployment, without changing the JAR. We can also create multiple instances of application by launching the same JAR multiple times with different ports.

Programatic Way

Apart from configurations and command line arguments, we can also set the port number programatically. With Spring Boot there are two ways to do so.

Spring Application Class

Each Spring Boot application is bootstrapped from and application class, which has a public static void main (String[] arg) method. We can set the server port in this method.

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.setDefaultProperties(Map.of("server.port", 9191));
        application.run(args);
    }
}Code language: Java (java)

Web Server Customizer

We can implement the WebServerFactoryCustomizer interface and modify the server port number as shown in the next example. We can created a @Configuration class which implements the interface.

package com.amitph.spring.dogs;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebServerFactoryCustomizerImpl 
        implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        factory.setPort(9191);
    }
}Code language: Java (java)

One thing to note that, of all different ways of setting server port, the programmatic approach of Web Server Customizer has the highest weightage.

Summary

This is the end of our short tutorial on how to change default port in Spring Boot Application.In this tutorial we learnt that by default the embedded tomcat in a Spring Boot Application always runs on a default port of 8080. However, there are multiple ways to configure the server port number of a Spring Boot application.

We began by using Configuration Properties to set a specific port or a random port to launch the application. We then, learnt to specify server port using command line argument. Finally we looked at the two programmatic approaches of port configuration. We also noted that out of all ways the programmatic approach of Web Server Configuration always takes precedence.