🏠Spring BootChanging Default Port Number in Spring Boot

Changing Default Port Number in Spring Boot

Spring Boot

The developers love spring Boot because it’s out of the box defaults. But sometimes, the developers may need to change these default values. Out of these, The most common one is to change the default port number of embedded web servers in spring boot.

So let’s check out the various ways to override the default port of a spring boot applications.

1. Using Configuration files

The most common and easier way to override default configurations is via properties files. To change the port number you should use the server.port property.

For example, you can change the port number to 8081 as shown below.

Code language: Properties (properties)

You can also use the same configuration in application.yml files.

server: port: 8081
Code language: YAML (yaml)

1.1. Profile Specific port change

It is also possible to change the port based on the active spring profile.

To demonstrate this, We can create an application-qa.properties with port 9090.

Code language: Properties (properties)

Also for uat, you could create another file called application-uat.properties with port 9091.

Code language: Properties (properties)

By running the same application with different profiles, you can switch between different ports. With no active profiles, Spring Boot will pick the default port.

2. Using Command Line arguments

There are two ways you can pass the port number to application via command line. They are,

  1. Program parameters.
  2. JVM parameters.

2.1 Port change using spring program parameters

Application or program parameters are the ones that you pass using double dash(–) while calling JAR files.

$ java -jar hello-world.jar --server.port=8082

Be aware that the program parameters will override values in the properties files.

2.2 Port change using JVM arguments

You could also pass the settings as JVM arguments as shown below.

java -Dserver.port=8080 -jar hello-world.jar

3. Using Environment Variables

If you add a SERVER_PORT environment variable to your OS settings, Spring Boot will pick the port number from there.

For windows,

set SERVER_PORT=8081 java -jar hello-world.jar
Code language: Bash (bash)

For Linux/Mac,

export SERVER_PORT=8081<br>java -jar hello-world.jar
Code language: Bash (bash)

Twilio has a good article for env variables on Windows/Linux and Mac if you would like to read more.

Also note that this approach is best when used with Docker images as it is easier to pass environment variables to a docker runtime instead of external properties files.

4. Programatical approach to change the port

At the end, Spring boot needs to create the server with a given port. But if we could provide a WebServerFactoryCustomizer bean with a hard coded port number.

@Bean WebServerFactoryCustomizer<ConfigurableWebServerFactory> getWebServerFactoryCustomizer() { return factory -> factory.setPort(9090); }
Code language: Java (java)

Even though this is possible, Never do this in your production code. This is because you can’t override this value without another code change. Pick any one of those approaches from the above list.

5. The order of evaluation

As we have seen different approaches here, There may be situations where you may need to club these options together. In such cases, Spring Boot will evaluate the port number in the following order. Higher on this list is preferred the most.

  1. Hard-Coded Value from WebServerFactoryCustomizer
  2. Commandline arguments
  3. OS environment variables
  4. A profile specific application.properties
  5. application properties

There are also minor variants to these approaches which you can find at the external configurations section of spring boot documentation.

5. Summary

To summarize, we learned 4 different ways in which we can change the server port of a spring boot application. If you liked this article please give a read on the following writeups.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *