Tuesday , November 21 2017
Home / Spring Boot / How to change Spring Boot Tomcat Port Number

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 server.port 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 chandrashekhar

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

Recommended

Spring Boot FileUpload Ajax Example

In this tutorials, we are going to see how to work with Spring Boot Fileupload …

Leave a Reply

Your email address will not be published. Required fields are marked *