Spring Boot Change Default Server Port
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.
Contents
- 1. Using Property File (.properties/.yml)
- 2. Using java Command with --server.port or -Dserver.port
- 3. Using java Command with --port or -Dport in Short
- 4. Using SERVER_PORT as OS Environment Variable
- 5. Using SERVER.PORT with SpringApplication Programmatically
- 6. Using EmbeddedServletContainerCustomizer
- 7. Using Eclipse Run Configurations with Environment Variable
- 8. Using Eclipse Run Configurations with Arguments
- 9. References
1. 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
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
java -jar -Dserver.port=8585 my-app.jar
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}
application.yml
server: port: ${port:8282}
Using --port
java -jar my-app.jar --port=8585
java -jar -Dport=8585 my-app.jar
4. 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.
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); } }
6. 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
@Component public class ContainerCustomizerBean implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8585); } }
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
8. 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.
9. References
Change the HTTP portCustomizing embedded servlet containers