Change the default port in Spring Boot | Java Development Journal

Change the default port in Spring Boot

In this short post, we are going to cover the common ways to change the default port in Spring Boot.

 

Introduction

Spring Boot comes with the ability to give sensible defaults based on the application configuration. For example, in the case of web application, it comes with an embedded servlet container (tomcat) with defaults values configured for the servlet container. There are a few cases where we like to override these values with our custom values. In this post, we are going to learn the different options to change the default port in Spring Boot application.

 

1. Change Port using Properties Files

Spring Boot provides a flexible way to configure our application using a property file. To change the default port, we need to set the desired port number using server.port properties either through application.prioperties or <application.yml file.

Set server.port property in application.properties file.

server.port = 8090

Set server port property in <application.yml file

server:
     port: 8090

 

2. Programmatic Customization

We do have the option to programmatically configure your embedded servlet container. To do this, create a Spring bean which implements the WebServerFactoryCustomizer interface. 

@Component
public class CustomizationPort implements WebServerFactoryCustomizer < ConfigurableServletWebServerFactory > {

 @Override
 public void customize(ConfigurableServletWebServerFactory server) {
  server.setPort(9001);
 }

}

The port defined inside the CustomContainer always overrides the value defined inside application.properties.

 

3. Command Line Argument

We also have the option to set the port while starting our application. This is done by passing the argument through the command line.

java -Dserver.port=9001 -jar demo.jar
java -jar demo.jar –server.port=9001

 

4. Environment Specific Port

Spring Profiles provides a powerful and easy way to control code and configuration based on the environment. Using Spring Profiles it’s possible to segregate parts of our application and make it only available in certain environments. We can use the same feature to change the default port based on the environment or profile.

To use profile specific configuration files, we need to the naming convention of <application-{profile}.propertieswhere profile defines the name of the intended profile. Profile specific files will be loaded from the same location as application.properties file. Let’s take an example, where we want to run our server on 9001 port on the dev environment while on the production we like to run our application on 9022 port.

To do this using configuration files, we will define 2 configuration file namely application-production.propertiesand application-development.properties.

Set server.port property inapplication-development.properties file.

server.port = 9001

Set server.port property inapplication-production.properties file.

server.port = 9022

 

Summary

In this quick post, we cover the common ways to change the default port in Spring Boot. We learned how to do this using property file or by passing the port number through the command line argument.

Umesh

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

follow me on:

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of