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.
64 Answers
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 a random port use:
server.port=0
Similarly add application.yml
in /src/main/resources/
with:
server:
port: 8090
-
77When random port is used, port info can get with
@Value("${local.server.port}")
Jul 23, 2015 at 12:46 -
52Actually command line option is --server.port=8090 not -Dserver.port=8090. docs.spring.io/spring-boot/docs/current/reference/html/…– alpertAug 19, 2015 at 6:39
-
1As a compliment to this answer: According to the spring docs there are other paths you can put
application.properties
on. In my case that helped a lot.– sargasOct 2, 2015 at 19:37 -
17-Dserver.port=XXXX did not work for me. I used OS environment variable mode:
$ SERVER_PORT=8090 java -jar <path/to/my/jar>
Oct 8, 2015 at 7:38 -
17Both (1)
java -Dserver.port=XXXX -jar <path/to/my/jar>
and (2)java -jar <path/to/my/jar> --server.port=YYYY
works. The first command definesserver.port
system property, and the second command pass the property through the command line arguments (String... args
in themain
method). Moreover, if you run withjava -Dserver.port=XXXX -jar <path/to/my/jar> --server.port=YYYY
,YYYY
takes precedence overXXXX
, this is why Spring Boot Externalized Configuration is so charming.– tan9Nov 29, 2016 at 16:28
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
-
In STS 4 it is at run -> run configurations -> main, then scroll down to Table with Parameter Name and Value– serv-incApr 28, 2019 at 13:00
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.
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);
});
}
}
-
2This is working and very useful, when you have port in your own config file and want to set it during runtime.– XdgNov 8, 2015 at 9:48
-
6This was helpful when i needed to deploy an application to a AWS Elastic Beanstalk service, to get the port from an environment variable. Nov 19, 2015 at 12:51
-
This is super useful when all you want is a self-contained unit or integration test, +1. Apr 28, 2016 at 10:34
-
Very useful when the env variable for port is already defined under a different name.– higuaroAug 18, 2016 at 8:27
-
2Is'nt it the
@Configuration
instead of@Controller
? Please update if so.– LuckySep 13, 2016 at 11:52
If you would like to run it locally, use this -
mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8085'
As of Spring Boot 2.0, here's the command that works:
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085
clues were at:
-
5Starting from Spring Boot 2, you should use
spring-boot.run.jvmArguments
.– mapmApr 12, 2018 at 2:57 -
1
-
For the first command is valid wihout
''
too - thereforemvn spring-boot:run -Drun.jvmArguments=-Dserver.port=8085
works. Tested on SB 3.1.1 Jul 16, 2023 at 16:41
You can set port in java code:
HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);
new SpringApplicationBuilder()
.sources(SampleController.class)
.properties(props)
.run(args);
Or in application.yml:
server:
port: 9999
Or in application.properties:
server.port=9999
Or as a command line parameter:
-Dserver.port=9999
-
Using the HashMap will work only if no port is set in applications.properties or .yml.– MilgoApr 25, 2019 at 11:28
-
I have a 2nd "application" class as a utility, and I wanted to only set a different port in that one. Therefore all the config file based approaches were not helpful for me. Close to giving up I found your programatic approach. Thank you! Oct 8, 2021 at 12:43
In case you are using application.yml
add the Following lines to it
server:
port: 9000
and of course 0 for random port.
-
1this didn't seem to work. I used server.port in the application.yml and it worked Mar 5, 2015 at 13:11
-
Also, make sure to keep a space between the : and the value. Jul 20, 2022 at 16:28
As explained in Spring documentation, there are several ways to do that:
Either you set the port in the command line (for example 8888)
-Dserver.port=8888
or --server.port=8888
Example : java -jar -Dserver.port=8888 test.jar
Or you set the port in the application.properties
server.port=${port:4588}
or (in application.yml with yaml syntax)
server:
port: ${port:4588}
If the port passed by -Dport (or -Dserver.port) is set in command line then this port will be taken into account. If not, then the port will be 4588 by default.
If you want to enforce the port in properties file whatever the environment variable, you just have to write:
server.port=8888
Include below property in application.properties
server.port=8080
-
This doesn't work. What application.properties? Which properties can overwrite this one? How can I be sure Spring is picking it up? Dec 10, 2021 at 16:24
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
-
2
System.setProperty("server.port", 80);
is another way to achieve the same.– hd1Feb 11, 2017 at 20:34 -
@hd1, I added our answers to the main answer, check it out and modify as you se fit please Dec 30, 2019 at 14:40
if you are using gradle as the build tool, you can set the server port in your application.yml file as:
server:
port: 8291
If you are using maven then the port can be set in your application.properties file as:
server.port: 8291
-
-
What do Maven and Gradle have to do with whether you use a properties or YAML file? The build process (Maven) is completely disparate from the application framework (Spring Boot). Jun 3, 2021 at 9:01
To extend other answers:
There is a section in the docs for testing which explains how to configure the port on integration tests:
At integration tests, the port configuration is made using the annotation @SpringBootTest
and the webEnvironment
values.
Random port:
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
You can inject the value using @LocalServerPort
which is the same as @Value("${local.server.port}")
.
- Example:
Random port test configuration:
@RunWith(SpringRunner.class
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ExampleTest {
...
@LocalServerPort //to inject port value
int port;
}
Defined port:
@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)
It takes the value from server.port
if is defined.
- If is defined using
@TestPropertySource(properties = "server.port=9192")
, it overrides other defined values. - If not, it takes the value from
src/test/resources/application.properties
(if exists). - And finally, if it is not defined it starts with the default
8080
.
Example:
Defined port test configuration:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "server.port=9192")
public class DemoApplicationTests {
@Test
public void contextLoads() {
}
}
You can specify port by overriding EmbeddedServletContainerFactory
bean within your configuration (java based or xml). There you can specify port for used embedded servlet container. Please, see Spring Boot - Core "Embedded Servlet Container Support" paragraph and example there. Hope this helps.
-
1Direct link: docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/… Nov 13, 2014 at 10:24
-
Here's a current link to the programmatic override section: docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/…– jocullJun 14, 2018 at 15:33
There are three ways to do it depending on the application configuration file you are using
a) If you are using application.properties file set
server.port = 8090
b) If you are using application.yml file set server port property in YAML format as given below
server:
port: 8090
c) You can also Set the property as the System property in the main method
System.setProperty("server.port","8090");
There are many other stuffs you can alter in server configuration by changing application.properties. Like session time out, address and port etc. Refer below post
ref: http://docs.spring.io/spring-boot/docs/1.4.x/reference/html/common-application-properties.html
I used few of them as below.
server.session.timeout=1
server.port = 3029
server.address= deepesh
Add this in your application.properties
file
server.port= 8080
-
2
-
Why to add same asnwer one year later?!? and
server.port 8080
is wrong syntax for Java property file...– BetlistaFeb 8, 2018 at 13:19
As everyone said, you can specify in application.properties
server.port = 9000 (could be any other value)If you are using spring actuator in your project, by default it points to
8080, and if you want to change it, then in application.properties mention
management.port = 9001 (could be any other value)
In the application.properties
file, add this line:
server.port = 65535
where to place that fie:
24.3 Application Property Files
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:
A /config subdirectory of the current directory The current directory A classpath /config package The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
In my case I put it in the directory where the jar
file stands.
From:
By default spring boot app start with embedded tomcat server start at default port 8080. spring provides you with following different customization you can choose one of them.
NOTE – you can use server.port=0 spring boot will find any unassigned http random port for us.
1) application.properties
server.port=2020
2) application.yml
server:
port : 2020
3) Change the server port programatically
3.1) By implementing WebServerFactoryCustomizer interface - Spring 2.x
@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
// customize the factory here
factory.setPort(2020);
}
}
3.2) By Implementing EmbeddedServletContainerCustomizer interface - Spring 1.x
@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// customize here
container.setPort(2020);
}
}
4) By using command line option
java -jar spring-boot-app.jar -Dserver.port=2020
Indeed, the easiest way is to set the server.port property.
If you are using STS as IDE, from version 3.6.7 you actually have Spring Properties Editor for opening the properties file.
This editor provides autocomplete for all Spring Boot properties. If you write port and hit CTRL + SPACE, server.port will be the first option.
-
1
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.
Hope this one help
application.properties=> server.port=8090 application.yml=> server port:8090
Using property server.port=8080 for instance like mentioned in other answers is definitely a way to go. Just wanted to mention that you could also expose an environment property:
SERVER_PORT=8080
Since spring boot is able to replace "." for "_" and lower to UPPER case for environment variables in recent versions.
This is specially useful in containers where all you gotta do is define that environment variable without adding/editing application.properties
or passing system properties through command line (i.e -Dserver.port=$PORT
)
You can add the port in below methods.
Run -> Configurations section
In
application.xml
addserver.port=XXXX
-
1Do you mean
application.yml
and what IDE are you using? Please be specific.– LuckySep 13, 2016 at 11:56
Just have a application.properties
in src/main/resources
of the project and give there
server.port=****
where ****
refers to the port number.
1.1 Update via a properties file.
/src/main/resources/application.properties
server.port=8888
Update via a yaml file.
server:
port: 8888
EmbeddedServletContainerCustomizer
@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8888);
}
}
Providing the port number in application.properties file will resolve the issue
server.port = 8080
"port depends on your choice, where you want to host the application"
You can also use SERVER_PORT
environment variable to configure Spring Boot port. Just set the environment variable and restart the app:
set SERVER_PORT=9999 // on windows machine
export SERVER_PORT=9999 // on linux
Please note that if you do not set those environment variables system wide, you should run the boot app on the same session.
Mostly springboot runs on port:8080
because of embedded Tomcat. In some it may throw an error port 8080 already in use
. To avoid this kind of issues we can config the server port.
Using application.properties
add server.port=9898
On runtime config
run your application with below arguments.
spring-boot:run -Drun.jvmArguments='-Dserver.port=8081'
-
1This answer is outdated. You need to use the following arguments
mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8081'
, see stackoverflow.com/a/66370832/179014– asmaierNov 25, 2022 at 13:43 -
I've confirmed it works in SB 3.1.1 - BTW is not necessary use
''
, therefore ismvn spring-boot:run -Dspring-boot.run.jvmArguments=-Dserver.port=8081
works Jul 16, 2023 at 16:24
server.port=8080
in application properties. this configuration is inServerProperties.class
class underorg.springframework.boot.autoconfigure.web
.