How to Change Server Port in Spring Boot App

Spring boot is one of the most common ways to write REST API in Spring World . Most of the time we need to customize the default port of Spring Boot application . Default port of Spring Boot is 8080 .

Most of the time our application needs customizations most of the time we need to change default port of embedded server of spring boot It might be because some application is already running on the same port or we want to change the default port to some other port of our convenience .

There are numerous ways we can change the default port of spring boot application , we will run through all the different ways .

Must Read :-

  1. Spring Boot Profiling

Updating Spring Property File

I hope most of you are aware of this as it the most preferred and most common way of setting up customized port number in spring boot.

We can update server.port in application.properties this will change the default port and run on the specified port.

server.port=8084

Updating YAML Configurations

Now it is also second most convenient way in you application like the first one where we update the properties in application.yaml . I generally do not prefer this as I am not comfortable writing YAML files . let me know in comments in case you also think same .

server:
   port : 8084

Updating Port from Command Line

We can also update the port from command line by specifying argument -Dserver.port

java -jar -Dserver.port=8080 <jar-name>.jar

You can also use Jar Launcher command as an alternative to run spring boot application . Have a look at variety of ways to run a Spring Boot application .

java -Dserver.port=8084 -Xdebug -Xnoagent -Djava.compiler=NONE org.springframework.boot.loader.JarLauncher 

Spring Profiling based Port update

If your spring bot application is having multiple profiles like dev and prod , and you have separate application-dev.properties and application-prod.properties .

Then you can specify server port in each of the properties like below and your spring boot application will run on different ports on different environments .

server.port=9090

If you are not sure how to do profiling in Spring boot applications , have a look at here .

Writing Custom Code to change Server Port

We are going to write a custom code to override the tomcat default server configurations and change port . Lets see how we can configure Tomcat Servlet Customizer . Have a look How to Deploy React JS in Tomcat Server .

@Component
public class TomcatCustomizer 
  implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
 
    @Override
    public void customize(ConfigurableWebServerFactory tomcatFactory) {
        tomcatFactory.setPort(8086);
    }
}

We have created a Custom tomcat configuration to override the PORT , but what if we want to customize default server.port and change the server PORT default Value . Let me know in comments section how to do that , in case you are not aware i Will help you there .

Conclusion

We have listed down variety of ways to change Server Port in Spring boot , but it gets confusing at times too . Hence it is important to understand the Order in which Spring boot gives Priority for the Custom Configurations which is a s follows.

  1. Custom Code Level Configurations
  2. Command Line Arguments
  3. Property Files

References :-

Spring Blog

Must Read :

How to Start Spring Boot Jar

You can start the spring boot jar by just typing
java -jar yourjar.jar . You can have a look at the article to find variety of other ways to run spring boot jar.
Best ways to Run spring boot jar via command line

How to Change Port In Spring Boot

Please add server.port=<DESIRED PORT> in your application.properties file .

How to Change Tomcat Port Number In Spring Boot

By default spring boot will run on port 8080 . If you want to change tomcat port add the configuration server.port=8080 in application.properties file

What is the default port of Tomcat in Spring Boot

8080

What is the default Server in Spring Boot

Apache Tomcat

Gitlab Reference

Leave a Comment