How to configure port for a Spring Boot application

前端 未结 30 1264
名媛妹妹
名媛妹妹 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:04

    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 (clues were here):

    mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085
    
    0 讨论(0)
  • 2020-11-22 14:11

    Hope this one help

    application.properties=> 
    
    server.port=8090
    
    application.yml=> 
    
    server
      port:8090
    
    0 讨论(0)
  • 2020-11-22 14:12

    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.

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

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

    In case you are using application.yml add the Following lines to it

    server:
         port: 9000
    

    and of course 0 for random port.

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

    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");
    
    0 讨论(0)
提交回复
热议问题