How to Change Default Tomcat Server Port in Spring Boot

(258 Views)


Tomcat Server takes by default 8080 port. i.e When you deploy your Spring Boot Application on Tomcat Server and want to run it, you have to enter an address like localhost:8080 or www.solutionfactory.in:8080 or ip-address/8080 But what if you want to change this port address(8080) to something else, then how to do it. Let?s see how to change default Tomcat Server Port 8080 in Spring Boot.

Change Tomcat Server Port 8080 in Spring Boot

In Spring Boot Application, you can change Tomcat Server Port by two ways,

  1. Using application.properties
  2. Using java code

  1. Change Port Using application.properties
  2. Spring boot introduced its default application properties named as "application.properties" file which is stored inside "src/main/resources" directory and it is auto detected without doing any spring based configurations.

    application.properties

    server.port = 1299

    server.port will change the Spring Boot tomcat's port number to 1299 but you are free to modify it as per your wish, if you run the application the tomcat server will take 1299 as its port number, you can check the port in the console and then execute the application on this port.

  3. Change Port Using Java Code
  4. Using Java code, we will have to create a simple java class which implements EmbeddedServletContainerCustomizer interface of Spring Boot, this is the interface that is provided for customizing auto-configured embedded servlet containers, and after implementing the interface EmbeddedServletContainerCustomizer we need to override its customize() method. Let us see this using an Example

    ServerConfig.java

    import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.stereotype.Component; @Component public class ServerConfig implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { // Change tomcat port to 1200 container.setPort(1200); } }

    Now when you run the application, the server will have port number 1200.

Note: If you use both application.properties and Java code config to change the port, Spring Boot will give preference to Java code. So, in our case if we use both application.properties and Java code Config, application's port number will be 1200

Solution Worked 3 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search




Search Tags