如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。
当前回答
在应用中提供端口号。属性文件将解决该问题
server.port = 8080
“端口取决于您的选择,您希望将应用程序托管在哪里”
其他回答
Spring-web模块默认提供了一个运行在端口号8080上的嵌入式tomcat服务器。
您可以修改如下-
A)如果你使用gradle,那么可以在你的应用程序中设置属性。yml:
server:
port: 8042
B)如果你正在使用maven,那么你可以在你的应用程序中设置属性。属性:
server.port: 8042
C)当你在自己的配置文件中有端口,并想在运行时设置它。
通过实现WebServerFactoryCustomizer接口- Spring 2.x
@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
// customize the factory here
factory.setPort(8042);
}
}
通过实现EmbeddedServletContainerCustomizer接口- Spring 1.x
@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// customize here
container.setPort(8042);
}
}
默认端口号是: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.
除了在申请时添加端口。属性,通过将属性文件方法与Spring配置文件相结合,您还可以轻松地实现针对不同环境的多个端口。具体来说,我们可以为每个环境创建一个属性文件。
例如,我们将有一个应用程序开发。包含以下内容的属性文件:
server.port=8081
然后您可以添加另一个应用程序-qa。属性文件使用不同的端口:
server.port=8082
由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
您可以在应用程序中配置端口。spring引导项目的资源文件夹中的属性文件。
server.port="port which you need"
推荐文章
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- 用Spring我可以做一个可选的路径变量吗?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- Java中的操作符重载
- 如何加速gwt编译器?
- 在Hibernate中重新连接分离对象的正确方法是什么?
- 应该……接住环内还是环外?
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?