How to configure port for a Spring Boot application

前端 未结 30 1242
名媛妹妹
名媛妹妹 2020-11-22 13:36

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.

相关标签:
30条回答
  • 2020-11-22 14:18

    You can add the port in below methods.

    1. Run -> Configurations section

    2. In application.xml add server.port=XXXX

    0 讨论(0)
  • 2020-11-22 14:20
    1. As everyone said, you can specify in application.properties
      server.port = 9000 (could be any other value)

    2. 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)

    0 讨论(0)
  • 2020-11-22 14:22

    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
    
    0 讨论(0)
  • 2020-11-22 14:22

    To extend other answers:

    There is a section in the docs for testing which explains how to configure the port on integration tests:

    • 41.3 Testing Spring Boot applications
    • 41.3.3 Working with random ports

    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() {
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 14:22

    Just have a application.properties in src/main/resources of the project and give there

    server.port=****
    

    where **** refers to the port number.

    0 讨论(0)
  • 2020-11-22 14:23

    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.

    0 讨论(0)
提交回复
热议问题