[软件设计/软件工程] 将上下文路径添加到 Spring Boot 应用程序

[复制链接]
发表于 2022-5-5 08:58:41
问题
我正在尝试以编程方式设置 Spring Boot 应用程序的上下文根。上下文根的原因是我们想从 localhost:port/{app_name} 访问应用程序并将所有控制器路径附加到它。

这是 Web 应用程序的应用程序配置文件。
  1. @Configuration
  2. public class ApplicationConfiguration {

  3.   Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class);

  4.   @Value("${mainstay.web.port:12378}")
  5.   private String port;

  6.   @Value("${mainstay.web.context:/mainstay}")
  7.   private String context;

  8.   private Set<ErrorPage> pageHandlers;

  9.   @PostConstruct
  10.   private void init(){
  11.       pageHandlers = new HashSet<ErrorPage>();
  12.       pageHandlers.add(new ErrorPage(HttpStatus.NOT_FOUND,"/notfound.html"));
  13.       pageHandlers.add(new ErrorPage(HttpStatus.FORBIDDEN,"/forbidden.html"));
  14.   }

  15.   @Bean
  16.   public EmbeddedServletContainerFactory servletContainer(){
  17.       TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
  18.       logger.info("Setting custom configuration for Mainstay:");
  19.       logger.info("Setting port to {}",port);
  20.       logger.info("Setting context to {}",context);
  21.       factory.setPort(Integer.valueOf(port));
  22.       factory.setContextPath(context);
  23.       factory.setErrorPages(pageHandlers);
  24.       return factory;
  25.   }

  26.   public String getPort() {
  27.       return port;
  28.   }

  29.   public void setPort(String port) {
  30.       this.port = port;
  31.   }
  32. }
复制代码

这是主页的索引控制器。
  1. @Controller
  2. public class IndexController {

  3.   Logger logger = LoggerFactory.getLogger(IndexController.class);

  4.   @RequestMapping("/")
  5.   public String index(Model model){
  6.       logger.info("Setting index page title to Mainstay - Web");
  7.       model.addAttribute("title","Mainstay - Web");
  8.       return "index";
  9.   }

  10. }
复制代码

应用程序的新根目录应位于 localhost:12378/mainstay ,但仍位于 localhost:12378 。

我错过了什么导致 Spring Boot 在请求映射之前不附加上下文根?

回答
你为什么要提出自己的解决方案。 SpringBoot 已经支持这一点。

如果您还没有,请将 application.properties 文件添加到 src\main\resources 。在该属性文件中,添加 2 个属性:
  1. server.contextPath=/mainstay
  2. server.port=12378
复制代码

更新(Spring Boot 2.0)

从 Spring Boot 2.0 开始(由于 Spring MVC 和 Spring WebFlux 支持), contextPath 已更改为以下内容:

server.servlet.contextPath=/mainstay

然后可以删除自定义 servlet 容器的配置。如果需要对容器做一些后期处理,可以在配置中添加一个 EmbeddedServletContainerCustomizer 实现(比如添加错误页面)。

基本上,application.properties 中的属性充当默??认值,您可以通过在交付的工件旁边使用另一个 application.properties 或通过添加 JVM 参数 ( -Dserver.port=6666 ) 来覆盖它们。

另请参阅参考指南,尤其是“功能”部分。部分。

该类实现 ServerProperties 。 EmbeddedServletContainerCustomizer 的默认值为 contextPath 。在代码示例中,您直接在“”上设置 contextPath。 .接下来 TomcatEmbeddedServletContainerFactory 实例将处理此问题并将其从您的路径重置为 ServerProperties 。 (此行执行“”检查,但它总是失败,因为默认值为 null 并将上下文设置为“”,覆盖你的)。





上一篇:SELECT查看临时表的结果集s)数据?
下一篇:如何在本地 kubernetes 集群上设置 Jenkins-x?

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表