Spring Boot

How to change Spring Boot Tomcat Port Number

In this tutorials, I am going to show you how to change the Spring Boot Tomcat Port number.

In the previous tutorials I have shown you how to create a simple Spring Boot Hello World application with default port (8080).  You can download the application and make the below changes.

Here I am taking the same example but trying to change the default port number 8080 to 8085.

Spring Boot Tomcat Port  :

In Spring Boot we can change the server port in 3 ways.

  • By updating the application.properties file,
  • By configuring the Embedded Servlet Container and
  • By passing the arguments while running the application

Lets see the above 3 scenarios one by one,

application.properties

Spring Boot gives us a property called [highlight color=”green”]server.port[/highlight] which is used to change the tomcat port from properties file.

To use this, you can update the application.properties like below :

application.properties
server.port=8085

Configuring the Embedded Servlet Container :

We can also change the Spring Boot Port number by obtaining the ConfigurableEmbeddedServletContainer object. It allows us to change the runtime server properties like change the serverPort, contextPath, sessionTimeOut and etc.,

CustomContainer.java
package com.onlinetutorialspoint.spring.boot;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(8585);
    }
}

Arguments while running the application :

java -Dserver.port=8585 Application

Based on your requirement, you can use any one among 3 possibilities

Happy Learning :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment