How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.
By default, spring-web module provides an embedded tomcat server that is running under the port number 8080. If you need to change the port number of the application then go to application.properties
file and configure the port number by using server.port
property.
server.port= 9876
then your application is running under the port 9876.
Since Spring Boot provides various configuration externalization mechanism (through various PropertySource
implementations and/or processors wired into Environment
object in order), you can set any property outside of your jar archive through following methods:
Pass property through command line argument as application argument
java -jar <path/to/my/jar> --server.port=7788
From property in SPRING_APPLICATION_JSON
(Spring Boot 1.3.0+)
Define environment variable in U*IX shell:
SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar>
By using Java system property:
java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar>
Pass through command line argument:
java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}'
Define JVM system property
java -Dserver.port=7788 -jar <path/to/my/jar>
Define OS environment variable
U*IX Shell
SERVER_PORT=7788 java -jar <path/to/my/jar>
Windows
SET SERVER_PORT=7788
java -jar <path/to/my/jar>
Place property in ./config/application.properties
configuration file
server.port=7788
and run:
java -jar <path/to/my/jar>
Place property in ./config/application.yaml
server:
port: 7788
and run:
java -jar <path/to/my/jar>
Place property in ./application.properties
server.port=7788
and run:
java -jar <path/to/my/jar>
Place property in ./application.yaml
server:
port: 7788
and run:
java -jar <path/to/my/jar>
You can combine above methods all together, and the former configuration in the list take precedence over the latter one.
For example:
SERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788
The server will start and listen on port 7788.
This is very useful providing default properties in PropertySources with lower precedence (and usually packaged in the archive or coded in the source), and then override it in the runtime environment. And it is the design philosophy of Spring Boot:
Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
SERVER_NAME
to server.name
conversion was done by Relaxed Binding.
When you need a programatically way of doing it, you can set it during startup:
System.getProperties().put( "server.port", 80 );
SpringApplication.run(App.class, args);
This might help for things like environment dependent port. Have a nice day
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.
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
The server will start with 8585 port. To get random server port, assign 0 to the property.
server.port = 0
Now spring boot will start the 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 the argument as follows.
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 the port as the argument then by default server will start with 8282. If we want a 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 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.humoyun;
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.
You can set that in application.properties under /src/main/resources/
server.port = 8090
In application.properties
file present in resources:
server.port=8082