In this post we will see how to change 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 one shown below:
***************************
APPLICATION FAILED TO START
***************************
Description:
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 root of 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 /config in classpath
- /config subdirectory 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 |
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 below 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 2 |
SET SERVER_PORT=8081 java -jar springBootApp.jar |
1 |
SERVER_PORT=8081 java -jar springBootApp.jar |
We can also configure the servlet container’s port programmatically:
1. Prior to Spring Boot 2, we can use 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}")
.
Thanks for reading.
Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
Leave a Reply