Configure port in a Spring Boot application
This post will discuss how to change the server port for a Spring Boot application to listens to HTTP requests.
By default, the embedded server of a Spring Boot application listens for HTTP requests on port 8080. If your application fails to start on port 8080, that means that port is already in use, and we would get a dedicated error message similar to the one shown below:
APPLICATION FAILED TO START
***************************
Description:
The embedded servlet container failed to start. Port 8080 was already in use.
Action:
Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port.
We can resolve this issue by changing the port of our application. There are several ways to achieve this, as discussed below:
1. Property file
In Spring Boot, we can set the server port in application.properties located in /src/main/resources/ directory, as shown in the following example:
|
1 |
server.port=8081 |
2. YAML file
We can also store external properties in a YAML file, which is a superset of JSON. Create a file named application.yml and put it in the root of the classpath. The preceding example corresponds to the following YAML file:
|
1 2 |
server: port: 8081 |
Both application.properties and application.yml can be placed in four pre-determined locations:
- root of the classpath
- current directory
- package
/configin classpath /configsubdirectory of the current directory
3. System property
We can also set server.port as System property inside the main method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Main { public static void main(String[] args) { System.setProperty("server.port", "8081"); // or, use // System.getProperties().put("server.port", 8081); SpringApplication.run(Main.class, args); } } |
4. Change VM Options
We can also define JVM system property while starting Spring Boot application through command line argument, as shown below:
|
1 |
java -Dserver.port=8081 -jar springBootApp.jar |
Here, springBootApp.jar is our executable JAR. Otherwise, in IntelliJ IDEA, go to Run -> Edit Configurations -> VM options, and add the following argument to the VM
|
1 |
-Dserver.port=8081 |
We can also pass server.port property as an application argument, as shown below:
|
1 |
java -jar springBootApp.jar --server.port=8081 |
5. Define OS environment variable
We can also use SERVER_PORT as an OS environment variable in Unix/windows. This works since Spring Boot uses some relaxed rules for binding Environment properties to @ConfigurationProperties beans.
1. Windows
|
1 2 |
SET SERVER_PORT=8081 java -jar springBootApp.jar |
2. UNIX
|
1 |
SERVER_PORT=8081 Java -jar springBootApp.jar |
We can also configure the servlet container’s port programmatically:
1. Before Spring Boot 2, we can use the EmbeddedServletContainerCustomizer interface.
|
1 2 3 4 5 6 7 8 9 10 11 |
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; @Component public class CustomContainer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8081); } } |
2. With Spring Boot 2, the EmbeddedServletContainerCustomizer interface is replaced by WebServerFactoryCustomizer,
|
1 2 3 4 5 6 7 8 9 10 |
import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; public class ServletConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { public void customize(ConfigurableServletWebServerFactory factory) { factory.setPort(8081); } } |
Tips:
For completely turning off the HTTP endpoints, use server.port=-1. This would still create a WebApplicationContext and can be useful for testing.
To assign a random HTTP Port, use server.port=0. To discover the random HTTP port at runtime, use @Value("${local.server.port}").
That’s all about configuring a port in a Spring Boot application.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)