spring : 绕过特定控制器的 contextPath

spring : bypass contextPath for a specific controller

我正在构建一个 Spring Cloud Config 服务器,我使用 属性 server.contextPath: /configServer 但我也希望我的服务器在对 /ping 的任何请求时以 200 响应(不是/configServer/ping)。 有什么方法可以绕过特定 RestController 的 contextPath 属性(或任何其他实现方法)?

谢谢。

也许添加一个 ServletRegistrationBean。这将允许您监听来自 /ping/*.

的请求
@Bean
public ServletRegistrationBean pingServletDispatcher() {
    DispatcherServlet dispatcherServlet = new DispatcherServlet();

    //XmlWebApplicationContext applicationContext = new XmlWebApplicationContext();
    //applicationContext.setConfigLocation("classpath:/META-INF/spring/webmvc-context.xml");
    //dispatcherServlet.setApplicationContext(applicationContext);

    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/ping/*" /* or /* / */);
    servletRegistrationBean.setName("ping");

    return servletRegistrationBean;
}

我找到了问题的解决方案:

spring property spring.cloud.config.server.prefix 正是我所寻找的。此前缀类似于 contextPath 但仅适用于配置服务器,因此我可以为 /ping 映射编写自定义控制器,同时配置服务器为 /configServer.

提供所有请求

更新:

示例代码:

这是响应/ping的控制器

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;    

@RestController
public class PingController {

    @RequestMapping(value = "/ping", method = RequestMethod.GET)
    public void respondToPing() {
        return;
    }

}

在应用程序属性中(我使用 yml):

...
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        prefix: /api/configuration
...

我不能共享整个配置文件,但值得一提的是它不包括 server.contextPath 属性.

这将导致这些映射:

2018-11-14 15:45:53.208  INFO 13412 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/ping],methods=[GET]}" onto public void com.coral.epos2.config.server.controllers.PingController.respondToPing()
2018-11-14 15:45:53.215  INFO 13412 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-11-14 15:45:53.218  INFO 13412 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-11-14 15:45:53.333  INFO 13412 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/configuration/encrypt],methods=[POST]}" onto public java.lang.String org.springframework.cloud.config.server.encryption.EncryptionController.encrypt(java.lang.String,org.springframework.http.MediaType)
2018-11-14 15:45:53.336  INFO 13412 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/configuration/encrypt/{name}/{profiles}],methods=[POST]}" onto public java.lang.String org.springframework.cloud.config.server.encryption.EncryptionController.encrypt(java.lang.String,java.lang.String,java.lang.String,org.springframework.http.MediaType)
2018-11-14 15:45:53.339  INFO 13412 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/configuration/decrypt/{name}/{profiles}],methods=[POST]}" onto public java.lang.String org.springframework.cloud.config.server.encryption.EncryptionController.decrypt(java.lang.String,java.lang.String,java.lang.String,org.springframework.http.MediaType)
2018-11-14 15:45:53.342  INFO 13412 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/configuration/decrypt],methods=[POST]}" onto public java.lang.String org.springframework.cloud.config.server.encryption.EncryptionController.decrypt(java.lang.String,org.springframework.http.MediaType)
2018-11-14 15:45:53.345  INFO 13412 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/configuration/encrypt/status],methods=[GET]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.cloud.config.server.encryption.EncryptionController.status()
2018-11-14 15:45:53.348  INFO 13412 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/configuration/key],methods=[GET]}" onto public java.lang.String org.springframework.cloud.config.server.encryption.EncryptionController.getPublicKey()
...

我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <artifactId>config-server</artifactId>
    <groupId>foivaras</groupId>
    <modelVersion>4.0.0</modelVersion>
    <name>configuration server</name>
    <packaging>jar</packaging>
    <version>develop-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>4.7</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

希望对您有所帮助。