Spring Boot: Change Port for Web Application

后端 未结 6 1761
无人共我
无人共我 2021-02-20 08:41

I am currently trying to create a web application with Spring Boot. I need to host my application to localhost:8081. How do I change the port?

6条回答
  •  情深已故
    2021-02-20 09:19

    If you are using the embedded tomcat server, you can configure the EmbeddedServletContainerFactory bean yourself in your Application class annotated with @SpringBootApplication.

    This will give you options to customize your tomcat server, example configuration

    @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;
    } 
    

    You could also do the same for Jetty, using the JettyEmbeddedServletContainerFactory bean, or for Undertow using the UndertowEmbeddedServletContainerFactory .

    Official documentation found here : http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

提交回复
热议问题