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


当前回答

默认端口号是:8080,但我们可以在application.properties中自定义端口号 如下图所示

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port = 5050 -- #here we can give any port number.

其他回答

Via

application.properties

服务器。端口= 8082(或任何新的端口号)

via

application.yml

server
  port: 8082

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

对于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);
        });
    }
}

根据所使用的应用程序配置文件,有三种方法可以做到这一点

a)如果您正在使用应用程序。属性文件集

server.port = 8090

b)如果您正在使用应用程序。yml文件设置YAML格式的服务器端口属性,如下所示

server:
     port: 8090

c)也可以在main方法中将属性设置为System属性

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

默认端口号是:8080,但我们可以在application.properties中自定义端口号 如下图所示

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port = 5050 -- #here we can give any port number.

如果你在使用应用程序。yml添加以下行到它

server:
     port: 9000

当然,随机端口是0。