我正在尝试以编程方式设置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/主流,但它仍然位于localhost:12378。

是什么原因导致Spring Boot没有在请求映射之前追加上下文根?


当前回答

对于以下Spring引导2版本,您需要使用以下代码

server:
   context-path: abc    

对于Spring引导2+版本使用下面的代码

server:
  servlet:
    context-path: abc

其他回答

在Spring Boot 1.5中:

在application.properties中添加以下属性:

server.context-path=/demo

注意:/demo是你的上下文路径URL。

如果你正在使用应用程序。Yml和spring 2.0以上版本,然后按以下方式配置。

server:
  port: 8081
  servlet:
     context-path: /demo-api

现在所有的API调用都像http://localhost:8081/demo-api/一样

如果您正在使用Spring Boot,那么您不必通过Bean初始化来配置服务器属性。

相反,如果一个功能可用于基本配置,那么它可以在一个名为application的“属性”文件中设置,该文件应该位于应用程序结构的src\main\resources下。“properties”文件有两种格式

.yml . properties

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

在您的特定情况下,如果您决定使用扩展名.properties,那么您将有一个名为application的文件。src\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

我们可以使用WebServerFactoryCustomizer设置它。这可以直接添加到spring引导main方法类中,该类会启动spring ApplicationContext。

@Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
      webServerFactoryCustomizer() {
        return factory -> factory.setContextPath("/demo");
}

它必须是: server.servlet。Context-path = / demo 注意它没有引号只有'/'前面的值 这个值会被放入应用程序中。属性文件