How to configure port for a Spring Boot application
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.
Solution 1:
As said in docs either set server.port
as system property using command line option to jvm -Dserver.port=8090
or add application.properties
in /src/main/resources/
with
server.port=8090
For random port use
server.port=0
Similarly add application.yml
in /src/main/resources/
with
server:
port : 8090
Solution 2:
There are two main ways to change the port in the Embedded Tomcat in a Spring Boot Application.
Modify application.properties
First you can try the application.properties file in the /resources folder:
server.port = 8090
Modify a VM option
The second way, if you want to avoid modifying any files and checking in something that you only need on your local, you can use a vm arg:
Go to Run -> Edit Configurations -> VM options
-Dserver.port=8090
Additionally, if you need more information you can view the following blog post here: Changing the port on a Spring Boot Application
Solution 3:
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 fileserver.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.
Solution 4:
Also, you can configure the port programmatically.
For Spring Boot 2.x.x:
@Configuration
public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
public void customize(ConfigurableServletWebServerFactory factory){
factory.setPort(8042);
}
}
For older versions:
@Configuration
public class ServletConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
container.setPort(8012);
});
}
}