Spring Boot Change Default Server Port

By Arvind Rai, December 04, 2023
On this page, we will learn to change Spring Boot default server port.
1. When Spring Boot application starts, the embedded server starts with a default port. The embedded tomcat starts with 8080 port as default.
2. There are many ways to change default server port. We can use property file, system variables and arguments with java command to change embedded servlet container settings such as default server port. We can also change embedded servlet container settings programmatically.
3. In property file, we configure server.port property with server port value and we configure SERVER_PORT as system variable.

1. Using Property File (.properties/.yml)

To change server port using property file, we need to configure server.port property.
a. Using application.properties in classpath such as src\main\resources\application.properties
server.port = 8585 
Server will start with 8585 port. To get random server port, assign 0 to the property.
server.port = 0 
Now spring boot will start server on a port that is not being used currently by any server in the system.
b. Using application.yml in classpath such as src\main\resources\application.yml.
server:
  port: 8585 
Server will start with 8585 port. For random port, assign 0.
server:
  port: 0 

2. Using java Command with --server.port or -Dserver.port

Suppose we have an executable JAR named as my-app.jar, then while starting spring boot application using java command we can use argument as following.
Using --server.port
java -jar my-app.jar  --server.port=8585 
Using -Dserver.port
java -jar -Dserver.port=8585 my-app.jar 
Server will start with 8585 port.

3. Using java Command with --port or -Dport in Short

To make --server.port and -Dserver.port in short, we can remove server keyword and make it any short keyword such as --port and -Dport. We can use any short keyword. Here we are using port as short keyword. To achieve it we need to configure placeholder in property file as follows.
Using application.properties
server.port=${port:8282} 
Using application.yml
server:
  port: ${port:8282} 
If we do not pass port as argument then by default server will start with 8282. If we want different port, we need to pass desired port in argument. Suppose we have an executable JAR named as my-app.jar.
Using --port
java -jar my-app.jar --port=8585 
Using -Dport
java -jar -Dport=8585 my-app.jar 
Server will start with 8585 port.

4. Using SERVER_PORT as OS Environment Variable

We can change the spring boot default server port by setting SERVER_PORT as OS (such as Windows and Linux) environment variable. In my example I have Windows 7. Find the steps to configure environment variable.
Step 1: Right click on Computer icon then go to Properties -> Advanced System Settings -> Environment Variables and set variable as follows.
SERVER_PORT = 8585 
Step 2: Open the command prompt and build the project. Suppose we get an executable JAR as my-app.jar then run it using java command as follows.
java -jar my-app.jar 
Server will start with 8585 port.
If we want to run spring boot application from eclipse console, first restart the eclipse and then run the application.

5. Using SERVER.PORT with SpringApplication Programmatically

SpringApplication has a method as setDefaultProperties() that is used to change spring boot default properties. Suppose we want to change default port then we need to create a Map and put a port with SERVER.PORT key. Find the example.
MyApplication.java
@SpringBootApplication
public class MyApplication {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(MyApplication.class);
		Map<String, Object> map = new HashMap<>();
		map.put("SERVER.PORT", "8585");
		application.setDefaultProperties(map);
		application.run(args);
        }     
} 
Spring Boot will start the server with 8585 port.

6. Using EmbeddedServletContainerCustomizer

We can change embedded servlet container default settings registering a bean that implements EmbeddedServletContainerCustomizer interface. We need to override its customize() method. Find the example.
ContainerCustomizerBean.java
@Component
public class ContainerCustomizerBean implements EmbeddedServletContainerCustomizer {
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		container.setPort(8585);
	}
} 
Spring boot will start server with 8585 port.

7. Using Eclipse Run Configurations with Environment Variable

We can change spring boot default settings in eclipse by configuring Environment variable in run configurations.
Step-1: Right click on the class and go to Run As -> Run Configurations
Step-2: Click on the Environment tab and server port as follows.
SERVER_PORT = 8585 
Find the print screen of eclipse.
Spring Boot Change Default Server Port
Step-3: Run the application from eclipse console. Server will start with 8585 port.

8. Using Eclipse Run Configurations with Arguments

To run spring boot application in eclipse, we run the class containing main() method with SpringApplication as Java application. To change the default port, follow the steps.
Step-1: Right click on the class and go to Run As -> Run Configurations
Step-2: Click on the Arguments tab and configure server port as follows.
--server.port=8585 
Find the print screen of eclipse.
Spring Boot Change Default Server Port

Step-3: Run the application from eclipse console. Server will start with 8585 port.

9. References

Change the HTTP port
Customizing embedded servlet containers
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us