How to Change the default port in Spring Boot

In this short post, we are going to cover the common ways to change the default port in Spring Boot.

Introduction

Spring Boot comes with the ability to give sensible defaults based on the application configuration. For example, with web application, it comes with an embedded servlet container (tomcat) with defaults values configured for the servlet container. There are a few cases where we like to override these values with our custom values. In this post, we are going to learn the different options to change the default port in Spring Boot application.

1. Change Port in Spring Boot using Properties Files

Spring Boot provides a flexible way to configure our application using a property file. To change the default port, we need to set the desired port number using the server.port properties either through application.properties or application.yml file.Set server.port property in application.properties file.

server.port = 8090

Set server port property in application.yml file

server:
     port: 8090

Changing the default port in Spring Boot using application.properties is the most common and flexible way.

2. Programmatic Customization

We have the option to programmatically configure your embedded servlet container. To do this, create a Spring bean which implements the WebServerFactoryCustomizer interface. 

@Component
public class CustomizationPort implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

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

There is other option to set the property in the main @SpringBootApplication class:

@SpringBootApplication
public class ChangeApplicationPort {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ChangeApplicationPort.class);
        app.setDefaultProperties(Collections.singletonMap("server.port", "9001"));
        app.run(args);
    }
}

The port defined inside the Custom Container always overrides the value defined inside application.properties.

If you are still using Spring Boot older version (not 2.x), you can use the EmbeddedServletContainerCustomizer option to set the port number.

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

3. Command Line Argument

We also have the option to set the port while starting our application. This is done by passing the argument through the command line.

java -Dserver.port=9001 -jar demo.jar
java -jar demo.jar –server.port=9001

4. Environment Specific Port

Spring Profiles provides a powerful and easy way to control code and configuration based on the environment. Using Spring Profiles its possible to segregate parts of our application and make it only available in certain environments. We can use the same feature to change the default port based on the environment or profile.

To use profile specific configuration files, we need to the naming convention of application-{profile}.propertieswhere profile defines the name of the intended profile. It will load profile from the same location as application.properties file. Let’s take an example, where we want to run our server on 9001 port on the dev environment while on the production we like to run our application on 9022 port.

To do this using configuration files, we will define 2 configuration files, namely <em>application-production.properties</em>and <em>application-</em>development.properties.

Set server.port property in<em>application-</em>development.properties file.

server.port = 9001

Set server.port property in<em>application-production</em>.properties file.

server.port = 9022

5. Configuration Evaluation Order

Spring Boot follows a certain order to load the configuration properties. While overriding the changes, please remember the sequence used by Spring Boot to load these configurations:

  1. Embedded Server configuration.
  2. Command line arguments.
  3. Property file (application.properties file).
  4. Configuration at the @<a href="https://www.javadevjournal.com/spring-boot/spring-boot-application-annotation/" target="_blank" aria-label="SpringBootApplication (opens in a new tab)" rel="noreferrer noopener" class="rank-math-link">SpringBootApplication</a> class.

If you want Spring Boot to assigns random port for your application, set the port as 0 (server.port=0)

Summary

In this quick post, we cover the common ways to change the default port in Spring Boot. We learned how to do this using property file or by passing the port number through the command line argument. For most of the cases, using the application.properties or yml file is the most common option to change the default port in spring boot application, however, for many uses cases setting the port through command line or programmatically provides more flexibility. The source code for the article is available on the GitHub repository.

6 thoughts on “How to Change the default port in Spring Boot”

Comments are closed.

Scroll to Top