我正在尝试以编程方式设置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-boot已经支持这一点。

如果你还没有,添加一个应用程序。属性文件到src\main\resources。在该属性文件中,添加2个属性:

server.contextPath=/mainstay
server.port=12378

更新(Spring Boot 2.0)

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

server.servlet.context-path=/mainstay

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

基本上就是应用程序内部的属性。属性作为默认值,您总是可以通过使用另一个应用程序覆盖它们。属性,或者添加JVM参数(-Dserver.port=6666)。

请参见参考指南,特别是属性部分。

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


如果您正在使用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


我们可以在应用程序中设置它。属性是 API_CONTEXT_ROOT = / therootpath

我们在Java类中访问它,如下所述

@Value("${API_CONTEXT_ROOT}")
private String contextRoot;

正确的属性是

server.servlet.path

来配置DispatcherServlet的路径

and

server.servlet.context-path

来配置下面的应用程序上下文的路径。


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

server.servlet.context-path

server.contextPath = /中流砥柱

工作为我,如果我有一个战争文件在JBOSS。在包含jboss-web.xml的多个war文件中,它没有工作。我必须把jboss-web.xml与内容放在WEB-INF目录中

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
    <context-root>mainstay</context-root>
</jboss-web>

上下文路径可以直接集成到代码中,但这是不可取的,因为它不能被重用,所以写在应用程序中。属性文件 服务器。contextPath=/存放代码的文件夹名称 contextPath =您放置代码的文件夹名称/ 注意:仔细看斜线。


在Spring Boot 1.5中:

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

server.context-path=/demo

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


请注意“服务器”。上下文路径”或“server.servlet。Context-path”[从springboot 2.0开始。X]属性只有在部署到嵌入式容器(例如,嵌入式tomcat)时才有效。例如,如果将应用程序作为war部署到外部tomcat,这些属性将不起作用。

请看这里的答案:https://stackoverflow.com/a/43856300/4449859


您可以通过添加端口和contextpath轻松地在[src\main\resources] .properties文件和.yml文件中添加配置

应用程序。属性文件配置

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

应用程序。Yml文件配置

server:
port: 8084
contextPath: /context-path

我们还可以在spring引导中以编程方式更改它。

@Component
public class ServerPortCustomizer implements     WebServerFactoryCustomizer<EmbeddedServletContainerCustomizer > {

@Override
public void customize(EmbeddedServletContainerCustomizer factory) {
    factory.setContextPath("/context-path");
    factory.setPort(8084);
}

}

我们还可以用另一种方法

@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {SpringApplication application =     new pringApplication(MyApplication.class);
    Map<String, Object> map = new HashMap<>();
    map.put("server.servlet.context-path", "/context-path");
    map.put("server.port", "808");
    application.setDefaultProperties(map);
    application.run(args);
    }       
}

使用Java命令spring boot。X

java -jar my-app.jar --server.contextPath=/spring-boot-app     --server.port=8585 

使用Java命令spring boot。X

java -jar my-app.jar --server.servlet.context-path=/spring-boot-app --server.port=8585 

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

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

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


我们可以使用属性文件中的一个简单条目来更改上下文根路径。

application.properties

### Spring boot 1.x #########
server.contextPath=/ClientApp

### Spring boot 2.x #########
server.servlet.context-path=/ClientApp

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

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

如果您使用Spring Boot 2。如果你想在命令行中传递context path属性,你应该像这样输入double //:

--server.servlet.context-path=//your-path

这招对我很管用。


<!-- Server port-->

server.port=8080

<!--Context Path of the Application-->

server.servlet.context-path=/ems

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


你可以像下面这样在Spring Boot: 2.1.6中使用:

server.servlet.context-path=/api-path

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

server:
   context-path: abc    

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

server:
  servlet:
    context-path: abc

如果你正在使用spring-boot-starter-webflux:

spring:
  webflux:
    base-path: /api

我对天发誓……我每次都忘记这一点。


您可以将此属性添加到spring属性文件中

spring.data.rest.basePath=/your_path