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


当前回答

在我的情况下添加声明

server.port=${port:8081}

覆盖默认的tomcat服务器端口。

其他回答

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

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

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

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

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

通过编程方式,使用spring boot 2.1.5:

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(9000);
    }

}

类似于https://stackoverflow.com/a/36865796/1587329和https://stackoverflow.com/a/40799750/1587329, gradle的一行代码是

SERVER_PORT=9090 gradle bootRun

如果你正在使用spring命令行接口(CLI),使用——将命令与spring命令参数分开,来更改端口:

春天跑你好。Groovy -- --server.port=9000

spring-boot cli