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


当前回答

“服务器。Port =8080"将只工作,如果你运行应用程序作为一个jar通过main方法,

如果您通过tomcat容器将此应用程序作为war文件运行,则此配置将不起作用。

其他回答

服务器端口声明有两种类型

1.静态类型

   server.port=8080. // your port number

动态类型 server.port = 0。//随机生成端口号。 server.port = ${端口:0}

将此添加到应用程序中。属性文件

server.port= 8080

这个问题是Gradle Spring Port的第一个结果。

如果你使用gradle,你可以这样做,如果你有Spring Boot gradle插件已经应用:

bootRun {
    args += ["--server.port=[PORT]"]
}

想知道更复杂的答案,请看我的回答。

您可以在应用程序中配置端口。spring引导项目的资源文件夹中的属性文件。

server.port="port which you need"

此外,您还可以以编程方式配置端口。

对于Spring Boot 2.x.x:

@Configuration
public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
  public void customize(ConfigurableServletWebServerFactory factory){
    factory.setPort(8042);
  }
}

对于旧版本:

@Configuration
public class ServletConfig {
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {
            container.setPort(8012);
        });
    }
}