Spring Boot Change Context Path – Spring Boot改变上下文路径

最后修改: 2018年 4月 15日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Spring Boot, by default, serves content on the root context path (“/”).

Spring Boot,默认情况下,在根上下文路径(”/”)上保存内容。

While it’s usually a good idea to prefer convention over configuration, there are cases when we do want to have a custom path.

虽然通常情况下,宁愿选择惯例而不是配置是一个好主意,但在有些情况下,我们确实想有一个自定义的路径。

In this quick tutorial, we’ll cover the different ways of configuring it.

在这个快速教程中,我们将介绍配置它的不同方法。

2. Setting the Property

2.设置属性

Just like many other configuration options, the context path in Spring Boot can be changed by setting a property, server.servlet.context-path.

就像其他许多配置选项一样,Spring Boot中的上下文路径可以通过设置一个属性server.servlet.context-path来改变。

Note that this works for Spring Boot 2.x. For Boot 1.x, the property is server.context-path.

注意,这适用于Spring Boot 2.x。对于Boot 1.x,该属性是server.context-path

There are multiple ways of setting this property, so let’s look at them one by one.

设置这个属性有多种方法,让我们逐一看一下。

2.1. Using application.properties / yml

2.1.使用application.properties / yml

The most straightforward way of changing the context path is to set the property in the application.properties/yml file:

改变上下文路径的最直接的方法是在application.properties/yml文件中设置该属性。

server.servlet.context-path=/baeldung

Instead of putting the properties file in src/main/resources, we can also keep it in the current working directory (outside of the classpath).

与其把属性文件放在src/main/resources中,我们也可以把它放在当前工作目录中(classpath之外)。

2.2. Java System Property

2.2 Java系统属性

We can also set the context path as a Java system property before the context is even initialized:

我们也可以在上下文被初始化之前将上下文路径设置为一个Java系统属性。

public static void main(String[] args) {
    System.setProperty("server.servlet.context-path", "/baeldung");
    SpringApplication.run(Application.class, args);
}

2.3. OS Environment Variable

2.3.操作系统环境变量

Spring Boot can also rely on OS environment variables. On Unix based systems we can write:

Spring Boot也可以依靠操作系统的环境变量。在基于Unix的系统上,我们可以写。

$ export SERVER_SERVLET_CONTEXT_PATH=/baeldung

On Windows, the command to set an environment variable is:

在Windows上,设置环境变量的命令是。

> set SERVER_SERVLET_CONTEXT_PATH=/baeldung

The above environment variable is for Spring Boot 2.x.x.  If we have 1.x.x, the variable is SERVER_CONTEXT_PATH.

上面的环境变量是针对Spring Boot 2.x.x. 如果我们有1.x.x,该变量为SERVER_CONTEXT_PATH

2.4. Command Line Arguments

2.4.命令行参数

We can set the properties dynamically via command line arguments as well:

我们也可以通过命令行参数动态地设置属性。

$ java -jar app.jar --server.servlet.context-path=/baeldung

3. Using Java Config

3.使用Java配置

Now let’s set the context path by populating the bean factory with a configuration bean.

现在让我们通过向Bean工厂填充配置Bean来设置上下文路径。

With Spring Boot 2, we can use WebServerFactoryCustomizer:

通过Spring Boot 2,我们可以使用WebServerFactoryCustomizer

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

With Spring Boot 1, we can create an instance of EmbeddedServletContainerCustomizer:

通过Spring Boot 1,我们可以创建一个EmbeddedServletContainerCustomizer的实例。

@Bean
public EmbeddedServletContainerCustomizer
  embeddedServletContainerCustomizer() {
    return container -> container.setContextPath("/baeldung");
}

4. Priority Order of Configurations

4.配置的优先顺序

With this many options, we may end up having more than one configuration for the same property.

有了这么多的选项,我们最终可能会对同一财产有多个配置。

Here’s the priority order in descending order, which Spring Boot uses to select the effective configuration:

这里是降序的优先顺序,Spring Boot使用它来选择有效的配置。

  1. Java Config
  2. Command Line Arguments
  3. Java System Properties
  4. OS Environment Variables
  5. application.properties in Current Directory
  6. application.properties in the classpath (src/main/resources or the packaged jar file)

5. Conclusion

5.结论

In this brief article, we covered the different ways of setting the context path, or any other configuration property, in a Spring Boot application.

在这篇简短的文章中,我们介绍了在Spring Boot应用程序中设置上下文路径或任何其他配置属性的不同方法。