How to configure port for a Spring Boot application

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

提交回复
热议问题