1. Introduction

In this post, we will learn different ways of changing the embedded server port for a Spring Boot application. This post covers both Spring Boot 1 and 2 versions as there are different interfaces for configuring port number.

2. Using the Application Configuration File

2.1. application.properties

Spring provides server.port property that can be used to change the port.

server.port = 8888

2.2. application.yml

server:
  port : 8888

spring embedded server port

3. Using Interfaces

3.1. WebServerFactoryCustomizer – Spring Boot 2.x

Spring Boot 2.x provides ConfigurableWebServerFactory setPort method to change the embedded server port.

@Component
public class AppCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        factory.setPort(8888);
    }
}

3.2. EmbeddedServletContainerCustomizer – Spring Boot 1.x

In Spring Boot 1.x you can use EmbeddedServletContainerCustomizer to change embedded server port.

@Component
public class AppCustomizer implements EmbeddedServletContainerCustomizer {
 
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(8888);
    }
}

4. Conclusion

In this article, we have learned different ways of changing embedded server port for Spring Boot applications.

Enjoy!

Change Embedded Server Port under Spring Boot
Tagged on:                 

Satish Pandey

I am an expert Java Spring Angular developer with 10+ years of rich and varied experience in developing end-to-end Web Applications. I maintain this blog and publish articles in my free time to help the community. Email: satish@cloudtechpro.com

Leave a Reply

avatar
1024
  Subscribe  
Notify of