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


当前回答

在Spring引导应用程序中,有两种主要方法可以更改嵌入式Tomcat中的端口。

修改application.properties

首先,您可以尝试应用程序。属性文件在/resources文件夹:

server.port = 8090

修改虚拟机选项

第二种方法,如果你想避免修改任何文件和检入你只需要在你的本地文件,你可以使用vm arg:

执行—>编辑配置—>虚拟机选项

-Dserver.port=8090

此外,如果您需要更多信息,可以查看下面的博客文章:在Spring引导应用程序上更改端口

其他回答

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

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

默认端口号是: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.

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

1.静态类型

   server.port=8080. // your port number

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

在应用程序中。资源中的属性文件:

server.port=8082

希望这篇文章能有所帮助

application.properties=> 

server.port=8090

application.yml=> 

server
  port:8090