Tutorials / Tech blog / Geek Zone

Latest Posts

Tuesday, June 5, 2018

Spring Boot :: Change Default Port

As we all know Spring boot applications get started with embedded Tomcat server. The default application port is 8080. 

Property File

Spring boot provides out of the box pre-configured sensible defaults for many configuration properties. However many a times we might face a situation to alter these properties. One of the most common use case is altering the default port of the embedded tomcat server.

1
server.port=8081

The above change in application.properties file will ensure that the Spring Boot applications gets invoked on port 8081.

This is the most easiest way to customize Spring boot by overriding default values of the properties. The following change in application.properties file will do the trick.


This file can be located at src/main/resources directory of a Maven application.

Configuration via Code

We can also tweak the port pro-grammatically by configuring the relevant property in the main @SpringBootApplication class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@SpringBootApplication
public class SpringBootPortChangeApplication {

 public static void main(String[] args) {
  // SpringApplication.run(SpringBootPortChangeApplication.class, args);

  SpringApplication app = new SpringApplication(SpringBootPortChangeApplication.class);
  app.setDefaultProperties(Collections.singletonMap("server.port", "8082"));
  app.run(args);
 }
}

Command Line Arguments

We can also set the server.port property via command line, when packaging and running our application as a uber jar.


1
java -jar springbootportchange.jar --server.port=8083

Priority

So, overall the priority of these configurations is as follows:

  • Command Line Arguments
  • Property files
  • Configuration via Code

No comments:

Post a Comment