5 Ways : How to Change Default Port of Embedded Server in Spring Boot

In this short tutorial, I will be sharing different ways to configure or change the default port in Spring boot. For web applications, Spring Boot comes up with an embedded server i.e tomcat. The default port of tomcat is 8080. Changing the default port in an embedded server like tomcat is easy. Let's find out how.

Read Also : Spring Boot Hello World Example

1. Changing server port using Properties file

In Spring Boot application, if you are using properties file for configuration then its an one-line change.
You can change the server port by changing the value of server.port property. By default, its value is 8080. After changing the value, your application.properties file should look like this:

server.port = 8089 

Now the server will start on port 8089.

2. Changing server port using Yaml file

If you are using yaml file for configuration then you need to make the following change in application.yml file

server:
   port: 8089 

Both application.properties and application.yml file are loaded automatically by Spring boot if placed in the src/main/resources directory of a Maven application.

3. Changing server port using Command Line arguments

In case of a uber jar (contains java program and embeds its dependencies in a single jar file), using command-line arguments also, you can change the server port. There are two ways :

a. We can pass the argument --server.port=desired_port_number while running the application using command java -jar springbootapplication.jar as shown below :

java -jar springbootapplication.jar --server.port=8089

Or

b. you can change the port by passing the System property directly

java -jar -Dserver.port=8089 springbootapplication.jar 

4. Changing server port using System property

You can set server.port as System property using the main method as shown below :

package com.javahungry;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JavaHungry { 
 public static void main(String args[])
 {
  System.setProperty("server.port","8089");
   // or try
  // System.getProperties().put("server.port", 8089);
  SpringApplication.run(JavaHungry.class, args);
 }
}


Note : Application can be assigned random HTTP port, using server.port=0

If you want to find random HTTP port at runtime then use @Value("${local.server.port}").

5. Changing server port Programmatically

You can update the code, this will override properties and yaml settings.

For Spring Boot 2.x and above, use this code :

package com.javahungry;

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

@Component
public class AppCustomContainer 
  implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

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

For Spring Boot 1.x and below, use this code :


package com.javahungry;

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

@Component
public class AppCustomContainer implements EmbeddedServletContainerCustomizer {

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

6. Configuration Priority Order:

Embedded Server Configuration has the highest priority while changing port in Spring Boot. Command Line arguments take precedence over the properties or yaml file.

a. Programmatically (Embedded Server Configuration)
b. Command Line Arguments
c. System property 
d. Properties or Yaml file

In this post we learnt different ways to configure or change the default port in Spring Boot. If you have any questions or doubts then please mention in comments.

References :
Spring docs
Boot features external config

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry