How to configure port for a Spring Boot application

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

    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"

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

    Add this in your application.properties file

    server.port= 8080
    
    0 讨论(0)
  • 2020-11-22 14:02

    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)

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

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

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

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