How to change Spring Boot Tomcat Port Number

By | 2017-04-30T11:34:05+00:00 April 30th, 2017|Spring Boot|0 Comments

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.

[tie_list type=”checklist”]

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

[/tie_list]

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

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 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment