Home  >  Spring Boot

Spring Boot Change Default Server Port

By Arvind Rai, March 20, 2017
This page will walk through how to change spring boot default server port. When spring boot application starts, the embedded server such as Tomcat starts with a default port. The embedded tomcat starts with 8080 port as default. 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. In property file we configure server.port property with server port value and we configure SERVER_PORT as system variable. Now find the example.

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 

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.

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, then we need to pass desired port in argument as follows. 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.

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.

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
package com.concretepage;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@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.

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
package com.concretepage.bean;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;
@Component
public class ContainerCustomizerBean implements EmbeddedServletContainerCustomizer {
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
		container.setPort(8585);
	}
} 
Spring boot will start server with 8585 port.

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.

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. Find the console output.
 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-20 20:08:15.851  INFO 3888 --- [           main] com.concretepage.MyApplication           : Starting MyApplication on Renu-PC with PID 3888 (F:\arvind\PROJECT\mars\spring-boot\spring-boot-demo\target\classes started by Renu in F:\arvind\PROJECT\mars\spring-boot\spring-boot-demo)
2017-03-20 20:08:15.856  INFO 3888 --- [           main] com.concretepage.MyApplication           : No active profile set, falling back to default profiles: default
2017-03-20 20:08:15.955  INFO 3888 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@57f23557: startup date [Mon Mar 20 20:08:15 IST 2017]; root of context hierarchy
2017-03-20 20:08:17.833  INFO 3888 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8585 (http) 

References

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





Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us