如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。


当前回答

有三种方法

1设置服务器。应用中的端口属性。属性文件

server.port = 8090

2在应用中设置服务器端口属性。yml文件

server:
     port: 8090

3在“main method”中将属性设置为系统属性

System.setProperty("server.port","8090");

其他回答

如果你想在本地运行它,使用这个-

mvn spring-boot:run - dserver . jvmarguments ='-Dserver.port=8085'

从Spring Boot 2.0开始,下面的命令是有效的(线索在这里):

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085

你可以在java代码中设置port:

HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);

new SpringApplicationBuilder()
    .sources(SampleController.class)                
    .properties(props)
    .run(args);

或者在application.yml中:

server:
    port: 9999

或者在application.properties中:

server.port=9999

或者作为命令行参数:

-Dserver.port=9999

你可以在应用程序中设置。/src/main/resources/下的属性

server.port = 8090

您可以在应用程序中配置端口。属性文件或应用程序。Yaml文件,在src/main/resources中。

server.port=8080

大多数情况下,springboot运行在端口:8080上,因为使用了嵌入式Tomcat。在某些情况下,它可能抛出一个已经在使用的错误端口8080。为了避免这种问题,我们可以配置服务器端口。

使用application.properties

添加server.port = 9898

在运行时配置

使用以下参数运行应用程序。

spring-boot:跑-Drun.jvmArguments = ' -Dserver.port = 8081 '