如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。
当前回答
服务器。对于随机端口,Port = 0
服务器。端口= 8080为自定义8080端口
其他回答
由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
如文档中所说,要么设置服务器。端口作为系统属性使用命令行选项jvm -Dserver。端口=8090或添加应用程序。/src/main/resources/ with中的属性
server.port=8090
随机使用端口:
server.port=0
类似地,添加应用程序。Yml /src/main/resources/
server:
port: 8090
使用属性服务器。例如,端口=8080,就像在其他答案中提到的那样,绝对是一种方法。只是想提一下,你也可以暴露一个环境属性:
SERVER_PORT=8080
因为在最近的版本中,spring boot能够替换“_”中的“。”,并将环境变量的小写改为大写。 这在容器中特别有用,在容器中,你所要做的就是定义环境变量,而不需要添加/编辑应用程序。属性或通过命令行传递系统属性(即-Dserver.port=$PORT)
实际上,最简单的方法是设置服务器。端口属性。
如果你使用STS作为IDE,从3.6.7版本开始,你实际上有Spring Properties Editor来打开属性文件。
该编辑器为所有Spring Boot属性提供了自动补全功能。如果你写端口并按CTRL + SPACE,服务器。端口将是第一选择。
正如Spring文档中解释的那样,有几种方法可以做到这一点:
您可以在命令行中设置端口(例如8888)
-Dserver。Port =8888或——server.port=8888
例如:java -jar -Dserver。端口= 8888 test.jar
或者在application.properties中设置端口
server.port=${port:4588}
或(在申请中)。Yml和yaml语法)
server:
port: ${port:4588}
如果在命令行中设置了-Dport(或-Dserver.port)传递的端口,则将考虑该端口。如果不是,则端口默认为4588。
如果你想在属性文件中强制端口,不管环境变量是什么,你只需要写:
server.port=8888
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何找到哪个程序正在使用端口80在Windows?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder