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


当前回答

使用mvn shell命令行,spring-boot 2:

mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'

其他回答

实际上,最简单的方法是设置服务器。端口属性。

如果你使用STS作为IDE,从3.6.7版本开始,你实际上有Spring Properties Editor来打开属性文件。

该编辑器为所有Spring Boot属性提供了自动补全功能。如果你写端口并按CTRL + SPACE,服务器。端口将是第一选择。

在Config文件或应用程序属性中配置端口详细信息。

e.g.

port =8876

有三种方法

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

server.port = 8090

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

server:
     port: 8090

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

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

服务器。对于随机端口,Port = 0

服务器。端口= 8080为自定义8080端口

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

使用application.properties

添加server.port = 9898

在运行时配置

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

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