我目前正在尝试使用Spring Boot创建Web应用程序。 我需要将我的应用程序托管到localhost:8081端口。 我该如何更改端口?
我目前正在尝试使用Spring Boot创建Web应用程序。 我需要将我的应用程序托管到localhost:8081端口。 我该如何更改端口?
实际上,您想要更改server.port
,您可以按照描述的多种不同方式进行更改,如http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config所述。
示例:
命令行
java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar
等等
默认情况下,Spring Boot 使用端口8080,但是您可以通过在您的main()中添加以下代码行来更改端口:
System.getProperties().put( "server.port", *YOUR_PORT_NUMBER_GOES_HERE* );
< p >< em > 比如 @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);
}
或者
你可以在你的application.properties文件中像这样配置它:
server.port=8181
如果您的Spring Boot应用程序中没有application.properties文件,您可以创建一个。右键单击src/java/resources文件夹,选择New->Other->General并选择“文件”,然后命名为:application.properties。实际上,您想要更改服务器端口,并且可以按照以下描述的多种方式进行更改: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config
在您的application.properties中添加server.port=9000 。
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(9000);
factory.setSessionTimeout(10, TimeUnit.MINUTES);
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
return factory;
}
server.port=${PORT:8200}