How to change server port in spring boot How to change server port in spring boot

Spring boot web application using embedded server by default runs on port 8080. Following are the ways to change default server port from 8080 to say 9090

Follow any of the given five ways to change server port:-

1. application.properties
server.port = 9090
2. application.yml
server:
  port: 9090
3. command-line parameter
$ java -jar -Dserver.port=9090 spring-boot-app-1.0.jar

OR

$ java -jar spring-boot-app-1.0.jar --server.port=9090
4. SpringBootApplication main method
@SpringBootApplication
public class SpringBootDemoApplication {

    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(SpringBootDemoApplication.class);
        app.setDefaultProperties(Collections.singletonMap("server.port", "9090"));
        app.run(args);
    }
}
5. implement WebServerFactoryCustomizer interface
@Component
public class ServerPortCustomizer 
  implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
  
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        factory.setPort(9090);
    }
}