Spring Boot comes with a really easy way to manage spring application with minimal setup involvement. Here I’ll explain how we can change the default port of embedded tomcat server (8080) to any port you prefer in spring boot.

Topics inside this article,

Changing Default Port Using Property File

Spring boot uses 8080 port by default when we initiate a new project. We can use server.port property inside src/main/resources/application.properties to change this default port to another port which we prefer.

application.properties way,

server.port=8081

application.yml way,

server:
    port: 8089

Setup Environment Specific Ports Using Properties

Let’s assume we have a requirement on setting up different ports on different environments like below,

  1. 8081 – DEV
  2. 8082 – STAGING
  3. 8083 – PRODUCTION

So within the same application, we can have multiple property files for each environment to build this setup.

application-dev.properties

server.port=8081

application-staging.properties

server.port=8082

application-production.properties

server.port=8083

Then while you are running the application you could set whatever the profile you needs to have, then spring boot will refer the given property file which has given profile name while running the application.

Running application with dev profile just give following command,

with a gradle based project,

 $ ./gradlew bootRun --args='--spring.profiles.active=dev'

or else with a maven based project use following command,

$ mvn spring-boot:run -Dspring-boot.run.profiles=dev

or else if you are running your jar build file then use following command,

$ java -jar app.jar -Dspring.profiles.active=dev

Using Command Line Argument

We can set server-port using a command line argument as we set the active profile while running the application.

There are two ways to do that, You can use either way to get done the things the way you need.

Before running this arguments you should have correctly built jar file from your application.

$ java -jar my-app-1.0.0.jar --server.port=8081

or else

$ java -jar -Dserver.port=8081 my-app-1.0.0.jar

Note on Configuration File Priority

As of now you can understand multiple ways of injecting configuration into the spring boot application. But there is a order on how spring boot capture the configurations. That is,

  1. Embedded server config.
  2. Command Line arguments – ( -Dserver.port=8081 )
  3. Property files – ( application.properties )
  4. The configuration defined in @SpringBootApplication main class.

Conclusion

This is a really simple guide on how we can change the default port in spring boot application while developing and deploying on multiple environments. Hope you got a good understanding about how we can set up ports and profiles to manage those inside a spring boot applicatiomn.

If you are looking for spring boot based practical application development tutorials, just check our article series and comment on whatever the new things you need to see in our website.