This is one of the important question asked in the interviews. As we already know that we have embedded tomcat while we create Spring boot applications. The default port in which the tomcat runs is 8080.
Table of Contents
Via properties/Yaml file
If you want to change the port in application.properties then you can add the below property
server.port=8888
If you want to change the port in application.yml file then you can add the below property.
server:
port: 8888
Via Java Code
package com.springmicroservices;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;
@Component
public class PortChangeExample implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8888);
}
}
Leave a Reply