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