Change the default port in Spring Boot

Change the default port in Spring Boot

Hope you would have enjoyed and tried out building the Spring Boot application using my first blog on Booting Spring Boot for beginners. It helps beginners to kickstart creating a simple REST application using Spring Boot. If you have added the starter spring-boot-starter-web in your dependencies, Spring Boot by default has embedded Tomcat running in port 8080. But in many cases, one has to change the default configuration for our specific needs. This tutorial will talk about various ways to change the default port of the embedded server in Spring Boot.

The Apache Tomcat® software is an open-source implementation of the Jakarta Servlet, Jakarta Server Pages, Jakarta Expression Language, Jakarta WebSocket, Jakarta Annotations, and Jakarta Authentication specifications. These specifications are part of the Jakarta EE platform.

Property files

The easiest and straightforward way of changing the default port of the embedded server in Spring Boot is property files. Navigate to src/main/resources and create a file application.yml and add the following code.

We have changed the default port to 8081. Build and run the application and you can see the port has changed to 8081.

This can be also done in the application.properties file. Both the files are loaded by the Spring Boot placed in the specified directory.

Command-line arguments

Though setting the port using property files are sufficient in most cases, there are a few more ways to change the default port. One other way is using a command-line argument.

Before going to see how to do it, let us explore building the Spring Boot application in command-line instead of using IDE.

I am using Gradle to build the Spring Boot application. One can also use Maven as the build tool

Go to the root of your application and use the below command to build the application using Gradle.

./gradlew clean build

Once your build is successful you can find the binary jar generated in build/libs directory. Now we are ready to run the jar file using the below command-line argument to change the port of the Spring Boot application.

SpringBootApplication Configuration

Finally, let us have a look to change the default port programmatically using @SpringBootApplication to set the default property.

There is yet another way to change the default port programmatically in Spring 2.x by implementing the WebServerFactoryCustomizer interface.

Since there are several ways to change the default port of Spring Boot embedded sever, one has to understand the order of evaluation. Below is the order of evaluation of priority.

  1. Embedded server configuration

  2. Command-line arguments

  3. Property file

  4. @SpringBootApplication configuration

Now I think you should be in a position to try out to change the default port of the embedded server in the Spring Boot application.

Thank you for reading! If you have enjoyed it, please comment on it!


I am a seasoned engineering leader with extensive experience building enterprise cloud and mobile platforms while knitting together high-performing engineering teams. I inspire engineers to get the trains to run on time. I’m passionate about travel, collaboration, and shaping new engineers through my activities as a blogger, speaker, and course author. I have been sharing my thoughts on life, engineering, and productivity 🚀

Website|Blog|LinkedIn