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.
You can add the port in below methods.
Run -> Configurations section
In application.xml
add server.port=XXXX
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)
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
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.
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
You can inject the value using @LocalServerPort
which is the same as @Value("${local.server.port}")
.
Random port test configuration:
@RunWith(SpringRunner.class
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ExampleTest {
...
@LocalServerPort //to inject port value
int port;
}
@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)
It takes the value from server.port
if is defined.
@TestPropertySource(properties = "server.port=9192")
, it overrides other defined values.src/test/resources/application.properties
(if exists). 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() {
}
}
Just have a application.properties
in src/main/resources
of the project and give there
server.port=****
where ****
refers to the port number.
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.