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


当前回答

如果你正在处理启动项目,你想要配置端口,你可以在应用程序中输入。属性文件 注意:属性文件应该在src/main/resource下

春天的属性

server.port = 9999 如果您使用CMD,则遵循此命令 -Dserver.port = 9999 对于默认端口,它的server.port=0 确保没有端口正在使用此端口号

其他回答

你可以在java代码中设置port:

HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);

new SpringApplicationBuilder()
    .sources(SampleController.class)                
    .properties(props)
    .run(args);

或者在application.yml中:

server:
    port: 9999

或者在application.properties中:

server.port=9999

或者作为命令行参数:

-Dserver.port=9999

延伸其他答案:

文档中有一个测试部分解释了如何在集成测试中配置端口:

41.3测试Spring Boot应用程序 41.3.3使用随机端口


在集成测试中,端口配置是使用注释@SpringBootTest和webEnvironment值完成的。


随机端口:

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)

您可以使用与@Value("${local.server.port}")相同的@LocalServerPort注入值。

例子:

随机端口测试配置:

@RunWith(SpringRunner.class
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ExampleTest {
   ...
   @LocalServerPort //to inject port value
   int port;
}

定义端口:

@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)

它从服务器获取值。端口,如果已定义。

如果使用@TestPropertySource(properties = "server.port=9192")定义,它将覆盖其他定义的值。 如果不是,则从src/test/resources/application获取值。属性(如果存在)。 最后,如果没有定义它,则从默认的8080开始。

例子:

定义端口测试配置:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "server.port=9192")
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
    }

}

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

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

server.port = 8090

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

server:
     port: 8090

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

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

由Gradle运行:

在默认端口(8080)运行:./gradlew bootRun 运行在提供的端口(8888):./gradlew bootRun——args='——server.port=8888' 如果应用中有变量。PORT=8888 ./gradlew bootRun . properties文件

由Maven运行:

Run in default port(8080): mvnw spring-boot:run Run in provided port(8888): mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085' Run in provided port(8888): mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085' Run in provided port(8888) with other custom property: mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom" If we have any variable in the application.properties file named PORT, run this: SERVER_PORT=9093 mvn spring-boot:run

使用java -jar:

Create the .jar file: For Gradle: ./gradlew clean build. We will find the jar file inside: build/libs/ folder. For Maven: mvn clean install. We will find the jar file inside:target folder. Run in default port(8080): java -jar myApplication. jar Run in provided port(8888): java -jar myApplication.jar --port=8888 Run in provided port(8888): java -jar -Dserver.port=8888 myApplication.jar Run in provided port(8888) having variable SERVER_PORT in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar

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

1.静态类型

   server.port=8080. // your port number

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