Java 向Spring启动应用程序添加上下文路径

Java 向Spring启动应用程序添加上下文路径,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我试图以编程方式设置Spring启动应用程序上下文根。使用上下文根的原因是我们希望从localhost:port/{app_name}访问应用程序,并将所有控制器路径附加到它 以下是web应用程序的应用程序配置文件 @Configuration public class ApplicationConfiguration { Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class); @Value("

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

以下是web应用程序的应用程序配置文件

@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;
  }
}
应用程序的新根目录应该位于
localhost:12378/mainstay
,但它仍然位于
localhost:12378


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

为什么要尝试推出自己的解决方案。SpringBoot已经支持这一点

如果还没有,请将
application.properties
文件添加到
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
)来覆盖它们

另请参见本节


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

如果您使用的是Spring Boot,则不必通过Bean初始化来配置服务器属性

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

  • .yml

  • .properties

    ### Spring boot 1.x #########
    server.contextPath=/ClientApp
    
    ### Spring boot 2.x #########
    server.servlet.context-path=/ClientApp
    
  • 指定或设置配置的方式因格式而异

    在您的特定情况下,如果您决定使用扩展名
    .properties
    ,则在
    src\main\resources
    下会有一个名为
    application.properties
    的文件,其中包含以下配置设置

    server.port = 8080
    server.contextPath = /context-path
    
    另外,如果您决定使用
    .yml
    扩展名(即
    application.yml
    ),则需要使用以下格式(即
    YAML
    )设置配置:

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


    我们可以在
    应用程序.properties
    中将其设置为
    API\u CONTEXT\u ROOT=/therootpath

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

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

    正确的属性是

    server.servlet.path
    
    配置DispatcherServlet的路径


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

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

    server.servlet.context-path
    

    server.contextPath=/mainstay

    如果我在JBOSS中有一个war文件,这对我来说很有用。在包含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>
    
    
    中流砥柱
    
    上下文路径可以直接集成到代码中,但不建议这样做,因为它不能重复使用,所以请写入application.properties文件 server.contextPath=/放置代码的文件夹的名称 contextPath=放置代码的文件夹的名称/ 注意:仔细观察斜线。

    在Spring Boot 1.5中:

    应用程序中添加以下属性。属性

    server.context-path=/demo
    
    注意:
    /demo
    是您的上下文路径URL。

    请注意,“server.context path”或“server.servlet.context path”[从springboot 2.0.x开始]属性只有在部署到嵌入式容器(例如嵌入式tomcat)时才起作用。例如,如果将应用程序作为war部署到外部tomcat,则这些属性将不起作用


    请参见此处的答案:

    您可以通过添加端口和contextpath来轻松完成此操作,以便在[src\main\resources].properties文件和.yml文件中添加配置

    application.porperties文件配置

    server.port = 8084
    server.contextPath = /context-path
    
    server:
    port: 8084
    contextPath: /context-path
    
    application.yml文件配置

    server.port = 8084
    server.contextPath = /context-path
    
    server:
    port: 8084
    contextPath: /context-path
    
    我们还可以在SpringBoot中以编程方式更改它

    @Component
    public class ServerPortCustomizer implements     WebServerFactoryCustomizer<EmbeddedServletContainerCustomizer > {
    
    @Override
    public void customize(EmbeddedServletContainerCustomizer factory) {
        factory.setContextPath("/context-path");
        factory.setPort(8084);
    }
    
    使用java命令SpringBoot2.X

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

    如果您正在使用application.yml和2.0以上的spring版本,请按照以下方式进行配置

    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 ApplicationContext的spring boot主方法类中

    @Bean
        public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
          webServerFactoryCustomizer() {
            return factory -> factory.setContextPath("/demo");
    }
    
    @Bean
    公共WebServerFactoryCustomizer
    webServerFactoryCustomizer(){
    返回工厂->工厂.setContextPath(“/demo”);
    }
    
    如果您使用Spring Boot 2.x并希望在命令行中传递context path属性,则应按如下方式放置double//:

    --server.servlet.context-path=//your-path
    
    这对我在windows中运行很有效。

    
    
    <!-- Server port-->
    
    server.port=8080
    
    <!--Context Path of the Application-->
    
    server.servlet.context-path=/ems
    
    server.port=8080 server.servlet.context路径=/ems --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=/api-path
    
    server:
       context-path: abc    
    
    server:
      servlet:
        context-path: abc
    
    spring:
      webflux:
        base-path: /api