Spring Boot Change Default Server Port
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.
Contents
- Using Property File (.properties/.yml)
- Using java Command with --server.port or -Dserver.port
- Using java Command with --port or -Dport in Short
- Using SERVER_PORT as OS Environment Variable
- Using SERVER.PORT with SpringApplication Programmatically
- Using EmbeddedServletContainerCustomizer
- Using Eclipse Run Configurations with Environment Variable
- Using Eclipse Run Configurations with Arguments
- References
Using Property File (.properties/.yml)
To change server port using property file, we need to configureserver.port
property.
a. Using
application.properties
in classpath such as
src\main\resources\application.properties
server.port = 8585
server.port = 0
b. Using
application.yml
in classpath such as src\main\resources\application.yml.
server: port: 8585
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
java -jar -Dserver.port=8585 my-app.jar
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}
application.yml
server: port: ${port:8282}
Using --port
java -jar my-app.jar --port=8585
java -jar -Dport=8585 my-app.jar
Using SERVER_PORT as OS Environment Variable
We can change the spring boot default server port by settingSERVER_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
java -jar my-app.jar
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); } }
Using EmbeddedServletContainerCustomizer
We can change embedded servlet container default settings registering a bean that implementsEmbeddedServletContainerCustomizer
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); } }
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

Using Eclipse Run Configurations with Arguments
To run spring boot application in eclipse, we run the class containingmain()
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

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 portCustomizing embedded servlet containers