问题描述
我正在尝试以编程方式设置 Spring Boot 应用程序上下文根.上下文根的原因是我们希望从 localhost:port/{app_name}
访问应用程序,并将所有控制器路径附加到它.
I am trying to set a Spring Boot applications context root programmatically. The reason for the context root is we want the app to be accessed from localhost:port/{app_name}
and have all the controller paths append to it.
这是网络应用程序的应用程序配置文件.
Here is the application configuration file for the web-app.
@Configuration
public class ApplicationConfiguration {
Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class);
@Value("${mainstay.web.port:12378}")
private String port;
@Value("${mainstay.web.context:/mainstay}")
private String context;
private Set<ErrorPage> pageHandlers;
@PostConstruct
private void init(){
pageHandlers = new HashSet<ErrorPage>();
pageHandlers.add(new ErrorPage(HttpStatus.NOT_FOUND,"/notfound.html"));
pageHandlers.add(new ErrorPage(HttpStatus.FORBIDDEN,"/forbidden.html"));
}
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
logger.info("Setting custom configuration for Mainstay:");
logger.info("Setting port to {}",port);
logger.info("Setting context to {}",context);
factory.setPort(Integer.valueOf(port));
factory.setContextPath(context);
factory.setErrorPages(pageHandlers);
return factory;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
}
这是主页的索引控制器.
Here is the index controller for the main page.
@Controller
public class IndexController {
Logger logger = LoggerFactory.getLogger(IndexController.class);
@RequestMapping("/")
public String index(Model model){
logger.info("Setting index page title to Mainstay - Web");
model.addAttribute("title","Mainstay - Web");
return "index";
}
}
应用程序的新根应该在 localhost:12378/mainstay
,但它仍然位于 localhost:12378
.
The new root of the application should be at localhost:12378/mainstay
, but it is still located at localhost:12378
.
我遗漏了什么导致 Spring Boot 在请求映射之前没有附加上下文根?
What am I missing that is causing Spring Boot to not append the context root before the request mapping?
推荐答案
您为什么要尝试推出自己的解决方案.Spring-boot 已经支持了.
Why are you trying to roll your own solution. Spring-boot already supports that.
如果您还没有,请将 application.properties
文件添加到 srcmainesources
.在该属性文件中,添加 2 个属性:
If you don't already have one, add an application.properties
file to srcmainesources
. In that properties file, add 2 properties:
server.contextPath=/mainstay
server.port=12378
更新(Spring Boot 2.0)
从 Spring Boot 2.0 开始(由于 Spring MVC 和 Spring WebFlux 的支持),contextPath
已更改为以下内容:
As of Spring Boot 2.0 (due to the support of both Spring MVC and Spring WebFlux) the contextPath
has been changed to the following:
server.servlet.context-path=/mainstay
然后您可以删除自定义 servlet 容器的配置.如果您需要对容器进行一些后期处理,您可以在您的配置中添加一个 EmbeddedServletContainerCustomizer
实现(例如添加错误页面).
You can then remove your configuration for the custom servlet container. If you need to do some post processing on the container you can add a EmbeddedServletContainerCustomizer
implementation to your configuration (for instance to add the error pages).
基本上 application.properties
中的属性作为默认值,您可以随时通过在您交付的工件旁边使用另一个 application.properties
或通过添加 JVM 来覆盖它们参数(-Dserver.port=6666
).
Basically the properties inside the application.properties
serve as a default you can always override them by using another application.properties
next to the artifact you deliver or by adding JVM parameters (-Dserver.port=6666
).
另见参考指南,尤其是properties 部分.
类 ServerProperties
实现了 EmbeddedServletContainerCustomizer
.contextPath
的默认值是 ""
.在您的代码示例中,您直接在 TomcatEmbeddedServletContainerFactory
上设置 contextPath
.接下来,ServerProperties
实例将处理此实例并将其从您的路径重置为 ""
.(这一行 做了一个 null
检查,但由于默认是 ""
它总是失败并设置上下文到 ""
从而覆盖你的).
The class ServerProperties
implements the EmbeddedServletContainerCustomizer
. The default for contextPath
is ""
. In your code sample you are setting the contextPath
directly on the TomcatEmbeddedServletContainerFactory
. Next the ServerProperties
instance will process this instance and reset it from your path to ""
. (This line does a null
check but as the default is ""
it always fail and set the context to ""
and thus overriding yours).
这篇关于将上下文路径添加到 Spring Boot 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!