默认情况下,Spring Boot 应用程序通过上下文路径“/”访问,这是嵌入式服务器的默认路径,即我们可以直接在 http://localhost:PORT/
访问应用程序。
但在生产中,我们会将应用程序部署在某些上下文根下——这样我们就可以为其他地方引用 URL。此外,需要配置安全性,我们将需要应用程序的上下文根。
我们可以使用属性文件中的简单条目更改上下文根路径。
### Spring boot 1.x ######### server.contextPath=/ClientApp ### Spring boot 2.x ######### server.servlet.context-path=/ClientApp
在Spring boot 2.x中,我们可以自定义bean WebServerFactoryCustomizer
。我们可以使用它来更改应用程序上下文路径、端口、地址、错误页面等。
@Bean public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() { return factory -> factory.setContextPath("/ClientApp"); }
在 Spring boot 1.x 中,EmbeddedServletContainerCustomizer 接口用于自定义自动配置的嵌入式 servlet 容器。
@Component public class AppContainerCustomizer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setContextPath("/ClientApp"); } }
如果应用程序构建为 uber jar,我们也可以考虑此选项。
java -jar -Dserver.servlet.context-path=/ClientApp spring-boot-demo.jar
如果您知道在 spring boot 更改上下文路径 中完成此更改的任何其他方法,请告诉我。
快乐学习!!
标签2: Spring Boot地址:https://www.cundage.com/article/change-application-root-context-path.html