Spring boot by default assigns port 8080 to the embedded server(tomcat or any other) that it uses and starts internally.

This may not be what you want probably because some other application is running at this port or you don’t like defaults.

But, as all other Spring boot settings, port is also configurable and can be changed to some other value.

This article will explain different ways to change default server port in Spring boot.
Method 1: Using application properties
Add server.port property in application.properties file and assign it the port that you want the server to start as shown below.

server.port=9090

Method 2: Using yml file
If you application uses a .yml file(application.yml by default), then add below entries to it.

server:
   port : 8081

Method 3: From command line
Spring boot refers property server.port for customized port value. This property can also be set as JVM property while running a Spring boot application.
Thus, if your application is packaged as a jar, then setting server.port from command line can also be used as shown below.

java -jar -Dserver.port=9090 <app-jar-name>.jar

where -D flag is used for setting VM properties in java.
Method 4: Code customization Spring boot 2.x
If you can modify application code, then Spring boot provides utility interfaces that you can implement to change server configuration.

One such interface is WebServerFactoryCustomizer. It contains a method customize which is called automatically by Spring as a callback method at application startup.
customize receives an argument of type ConfigurableWebServerFactory.
Invoke its setPort method passing it the value of port that you want to assign.

Create a class that implements WebServerFactoryCustomizer interface and its customize method as shown below.

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class PortCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

  @Override
  public void customize(ConfigurableWebServerFactory factory) {
    factory.setPort(9090);
  }
  	
}

Annotate the class with @Component annotation so that it is discovered by Spring otherwise customize method will not be invoked.

If you are using Tomcat, you can also use ConfigurableTomcatWebServerFactory which is the child class of ConfigurableWebServerFactory as shown below.

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class PortCustomizer implements WebServerFactoryCustomizer<ConfigurableTomcatWebServerFactory> {

  @Override
  public void customize(ConfigurableTomcatWebServerFactory factory) {
    factory.setPort(9090);
  }
  	
}

This method will work if you have Spring boot 2.x.

Method 5: Code customization Spring boot 1.x
Spring 1.x defines an interface EmbeddedServletContainerCustomizer which declares a method customize. This method receives an argument of type ConfigurableEmbeddedServletContainer having a setPort method.

Create a class implementing EmbeddedServletContainerCustomizer and its customize method. customize will be called automatically by Spring as a callback.

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
class PortCustomizer implements EmbeddedServletContainerCustomizer {

  @Override
  public void customize(ConfigurableEmbeddedServletContainer container) {
    container.setPort(9092);
    
  }
  
}

Do not forget to apply @Component annotation. This method can only be used in Spring boot 1.x.
Method 6: Code customization in main class
As stated in Method 1, Spring boot uses value of server.port property for initializing embedded server port.
This property can also be set from Spring boot application main class which is annotated with @SpringBootApplication as shown below.

@SpringBootApplication
public class DemoApplication {

  /**
   * Main method
   * 
   * @param args
   */
  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(Demo1Application.class);
    // create properties
    Properties p = new Properties();
    // set custom port property
    p.put("server.port", 9090);
    // set properties for application
    app.setDefaultProperties(p);
    app.run(args);
  }
}

Order of resolution
If you have defined custom port using more than one methods in the same application, Spring boot defines an order of precedence so that it knows which value to consider.

The order is
1. Code configuration
2. Command line
3. .properties or .yml files
4. Main class(Method 5)

If Spring boot finds a custom port value at the first place, it will not consider the rest. Similarly, if you have set port in application.properties and main class, port defined in main class will be ignored.

Which method is the most convenient for you, do tell us in the comments below.
Hit the clap if the article was useful.

0

Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.