Spring Boot:更改Web应用程序的端口

Bri*_*ian 11 spring-boot

我目前正在尝试使用Spring Boot创建一个Web应用程序.我需要将我的应用程序托管到localhost:8081.如何更改端口?

sod*_*dik 23

实际上你想要改变server.port,你可以用许多不同的方式改变它,如http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config所述.

例子:

  • 在您的application.properties中(在jar中或jar外)
  • 命令行

    java -Dserver.port = $ PORT -jar target/demo-0.0.1-SNAPSHOT.jar

以及更多

  • 命令行选项是`--server.port = 8081` (5认同)

小智 6

实际上你想要改变server.port,你可以用http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config所描述的许多不同的方式来改变它.

server.port = 9000放在application.properties中


Top*_*Pug 5

默认情况下,spring boot使用端口8080,但是你可以通过在main()中添加以下代码行来改变端口,如下所示:

System.getProperties().put( "server.port", *YOUR_PORT_NUMBER_GOES_HERE* );  
Run Code Online (Sandbox Code Playgroud)

例如

@SpringBootApplication
public class MyClass {
public static void main(String[] args) {
    System.getProperties().put( "server.port", 8181 );  //8181 port is set here
    SpringApplication.run(MyClass.class, args);
}
Run Code Online (Sandbox Code Playgroud)

要么

您可以在application.properties文件中对其进行配置,如下所示:

server.port=8181
Run Code Online (Sandbox Code Playgroud)

如果你的spring-boot应用程序中没有application.properties文件,你可以继续创建一个.右键单击src/java/resources文件夹,然后转到New-> Other-> General并选择'File',然后命名为:application.properties

您可能需要的任何其他配置都列在此处https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html.这些属性也在application.properties文件中配置.