Change the default port in Spring Boot

Updated on September 5th, 2018

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



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 file.

server.port = 8090

Set server port property in <application.yml file

     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. 

public class CustomizationPort implements WebServerFactoryCustomizer < ConfigurableServletWebServerFactory > {

 public void customize(ConfigurableServletWebServerFactory server) {


The port defined inside the CustomContainer always overrides the value defined inside


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 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

Set server.port property file.

server.port = 9001

Set server.port property file.

server.port = 9022



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.

Java Development Journal

Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.

follow me on:

Leave a Reply

3 Comment threads
3 Thread replies
Most reacted comment
Hottest comment thread
4 Comment authors
Umesh AwasthiAnmolAristotle ZamoraAlexander Gonzalez Recent comment authors

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

newest oldest most voted
Notify of
Alexander Gonzalez
Alexander Gonzalez


Java Development Journal
Java Development Journal

Thanks.I am happy that it was helpful for you.

Aristotle Zamora
Aristotle Zamora

This is very helpful. Thanks for sharing!!!

Java Development Journal
Java Development Journal



this helped a lot. thanks for writing this

Java Development Journal
Java Development Journal

Happy to help Anmol!!