问题

我试图以编程方式设置Spring Boot应用程序上下文根。上下文根的原因是我们希望从localhost:port/{app_name}访问该应用程序并将所有控制器路径附加到它。

这是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;
  }
}

这是主页面的索引控制器。

@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

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


#1 热门回答(273 赞)

你为什么要尝试推出自己的解决方案。 Spring-boot已经支持了。

如果你还没有,请将aapplication.properties文件添加到src\main\resources。在该属性文件中,添加2个属性:

server.contextPath=/mainstay
server.port=12378

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

server.servlet.contextPath=/mainstay

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

基本上,作为默认值,application.properties中的属性可以通过使用另一个application.propertiesnext来覆盖它们,或者通过添加JVM参数(-Dserver.port=6666)来覆盖它们。

另见The Reference Guide,特别是properties

classServerProperties实现了EmbeddedServletContainerCustomizer。默认值为contextPathis""。在你的代码示例中,你将直接在TomcatEmbeddedServletContainerFactory上设置contextPath。接下来,ServerProperties实例将处理此实例并将其从你的路径重置为""。 (This line具有anull检查但是默认值为""it总是失败并将上下文设置为""并因此覆盖你的上下文)。


#2 热门回答(24 赞)

如果你使用的是Spring Boot,那么你不必通过Vean初始化来配置服务器属性。

相反,如果一个功能可用于基本配置,则可以在名为application的"属性"文件中进行设置,该文件应位于应用程序结构的1022365358之下。 "属性"文件有两种格式

  • .yml
  • .properties

指定或设置配置的方式因格式而异。

在你的特定情况下,如果你决定使用extension.properties,那么你将拥有一个名为application.propertiesundersrc\main\resources的文件,其中包含以下配置设置

server.port = 8080
server.contextPath = /context-path

OTOH,如果你决定使用.yml扩展(即application.yml),则需要使用以下格式设置配置(即:YAML):

server:
    port: 8080
    contextPath: /context-path

有关Spring Boot的更常见属性,请参阅以下链接:

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html


#3 热门回答(10 赞)

如果使用Spring Boot 2.0.0,请使用:

server.servlet.context-path

原文链接