添加Spring Boot应用程序的上下文路径

时间:2013-12-05 16:34:00

标签: java spring spring-mvc spring-boot

我正在尝试以编程方式设置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在请求映射之前没有附加上下文根?

19 个答案:

答案 0 :(得分:322)

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

如果您还没有,请将application.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容器的配置。如果您需要对容器进行一些后期处理,可以在配置中添加EmbeddedServletContainerCustomizer实现(例如添加错误页面)。

基本上application.properties中的属性作为默认值,您可以使用您提供的工件旁边的其他application.properties或通过添加JVM参数(-Dserver.port=6666)来覆盖它们。 / p>

另请参阅The Reference Guide,尤其是properties部分。

班级ServerProperties实施EmbeddedServletContainerCustomizercontextPath的默认值为""。在您的代码示例中,您可以直接在contextPath上设置TomcatEmbeddedServletContainerFactory。接下来,ServerProperties实例将处理此实例并将其从您的路径重置为""。 (This line执行null检查,但默认为"",它始终失败并将上下文设置为"",从而覆盖您的上下文。

答案 1 :(得分:27)

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

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

  1. .yml

  2. .properties

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

    在您的具体情况下,如果您决定使用扩展程序.properties,那么您将在application.properties下有一个名为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

答案 2 :(得分:15)

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

server.servlet.context-path

答案 3 :(得分:7)

正确的属性是

server.servlet.path

配置DispatcherServlet的路径

server.servlet.context-path

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

答案 4 :(得分:7)

请注意,“ server.context-path”或“ server.servlet.context-path” [从springboot 2.0.x开始]属性仅在您部署到嵌入式容器(例如嵌入式tomcat)时才起作用。例如,如果您要将应用程序作为战争部署到外部tomcat,这些属性将无效。

在此处查看此答案:https://stackoverflow.com/a/43856300/4449859

答案 5 :(得分:2)

我们可以在application.properties中设置它 API_CONTEXT_ROOT=/therootpath

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

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

答案 6 :(得分:2)

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

application.properties

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

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

答案 7 :(得分:1)

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

server:
   context-path: abc    

对于Spring boot 2+版本,请使用以下代码

server:
  servlet:
    context-path: abc

答案 8 :(得分:1)

在Spring Boot 1.5中:

application.properties中添加以下属性:

server.context-path=/demo

注意:/demo是您的上下文路径网址。

答案 9 :(得分:0)

如果您使用的是 spring-boot-starter-webflux,请使用:

spring:
  webflux:
    base-path: /api

我向上帝发誓......我每次都忘记这一点。

答案 10 :(得分:0)

您可以在Spring Boot:2.1.6中使用,如下所示:

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

答案 11 :(得分:0)

它必须是: server.servlet.context-path = /演示 请注意,它没有仅用'/'前面的值引起来 这个值会放在您的application.properties文件中

答案 12 :(得分:0)

<!-- Server port-->

server.port=8080

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

server.servlet.context-path=/ems

答案 13 :(得分:0)

如果您使用Spring Boot 2.x,并且想在命令行中传递上下文路径属性,则应将double //像这样放置:

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

这对我在Windows中运行的工作有用。

答案 14 :(得分:0)

我们可以使用WebServerFactoryCustomizer进行设置。可以直接将其添加到启动Spring ApplicationContext的spring boot main方法类中。

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

答案 15 :(得分:0)

如果您使用的是application.yml和2.0以上的春季版本,请按以下方式进行配置。

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

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

答案 16 :(得分:0)

您可以通过轻松添加端口和上下文路径来添加配置,以在[src \ main \ resources] .properties文件和.yml文件中添加配置

application.porperties文件配置

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

application.yml文件配置

server:
port: 8084
contextPath: /context-path

我们还可以在Spring Boot中以编程方式对其进行更改。

@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 1.X

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

使用Java命令spring boot 2.X

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

答案 17 :(得分:0)

<强> server.contextPath = /支柱

如果我在JBOSS中有一个war文件,

对我有用。在多个war文件中,每个文件都包含jboss-web.xml,但它不起作用。我不得不将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>

答案 18 :(得分:-1)

上下文路径可以直接集成到代码中,但不建议使用它,因为它无法重用,因此请在application.properties文件中写入 server.contextPath = /放置代码的文件夹的名称 contextPath =您放置代码的文件夹的名称/ 注意:仔细观察斜线。

相关问题