spring : bypass contextPath for a specific controller

巧了我就是萌 提交于 2020-01-04 15:17:05

问题


I am building a Spring Cloud Config server and I use the property server.contextPath: /configServer but I also want my server to respond with 200 on any request to /ping (not /configServer/ping). Is there any way to bypass the contextPath property for a specific RestController (or any other way to achieve that)?

Thanks.


回答1:


I found the solution to my problem:

There is the spring property spring.cloud.config.server.prefix which does exactly what I was looking for. This prefix is like contextPath but only for the configuration server hence I can write my custom controller for /ping mapping while configuration server serves all request to /configServer.

UPDATE:

sample code:

This is the controller to respond to /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;
    }

}

in the application properties (i use yml):

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

I cannot share the whole config file but it worths mentioning it doesn't include the server.contextPath property.

This will result in these mappings:

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()
...

My 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>

Hope that helps.




回答2:


Maybe add a ServletRegistrationBean. This would allow you to listen for requests coming to /ping or for /*.

@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;
}


来源:https://stackoverflow.com/questions/47137169/spring-bypass-contextpath-for-a-specific-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!