Hello Friends!, We are going to learn how to change Spring Boot Application Port.By Default Spring Boot application runs on embedded Tomcat Server on port 8080
In this tutorial, we will cover common ways to change port number of embedded server.
you can check our other post on the Maven project, on how to create Spring Boot Application using the below ways.
Application with Eclipse and Maven manually
Using Property Files or YAML File
this is easiest and simple way to change Spring Boot default properties.
for server port we need to change server.port
property value, let see how we can do that.
application.properties
create application.properties
file under src/main/resources/
folder.
server.port=8181
now application will start with 8181 port.
application.yaml
create application.yaml
file under src/main/resources/
folder.
server:
port: 8282
now application will start with 8282 port.
If Both files used in the application ,
application.properties
file will get used. Both the file will get loaded by Spring Boot application if it’s placed undersrc/main/resources/
Using Command Line Arguments
when running application as jar, we can provide server.port
argument while starting of Spring Boot application in two ways.
java -jar -Dserver.port=8383 springboot-first-application.jar
or
java -jar springboot-first-application.jar --server.port=8383
Programmatic Configuration
we can configure port programmatically in the following ways. as in SpringBootApplication
main class or implement WebServerFactoryCustomizer
.
@SpringBootApplication main class
while starting the application. we can provide port details to SpringApplication
class as shown in the below code.
package com.gyanideveloper.springboot;
import java.util.Collections;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(App.class);
application.setDefaultProperties(Collections.singletonMap("server.port", "8484"));
application.run(args);
}
}
using WebServerFactoryCustomizer
implement WebServerFactoryCustomizer to configure port.
package com.gyanideveloper.springboot;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;
@Component
public class WebServerCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8585);
}
}
This applies to Spring Boot 2.x
For Spring Boot 1.x
we can implement as follows.
@Configuration
public class ServletConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
container.setPort(8585);
});
}
}
Order Of Evaluation By Spring Boot application
as we have used different way to configure server port , let see order in which evaluated by Spring Boot Application.
the configurations priority is
- Embedded server configuration or WebServerFactoryCustomizer configuration.
- common line arguments
- properties file
- SpringBootApplication main class configuration
Use a Random Port
we can configure random port by setting server.port=0
, Spring scan for a free port (using OS natives to prevent clashes) .
server.port=0
Final Note
we have learn how to set server port in spring boot application.