spring:绕过contextPath为特定的控制器 [英] spring : bypass contextPath for a specific controller

查看:138
本文介绍了spring:绕过contextPath为特定的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)?

谢谢.

推荐答案

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

I found the solution to my problem:

其中有一个 spring属性spring.cloud.config.server.prefix 完全符合我的要求.此前缀类似于contextPath,但仅用于配置服务器,因此我可以为/ping映射编写我的自定义控制器,而配置服务器将所有请求提供给/configServer.

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.

示例代码:

这是响应/ping的控制器

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

}

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

in the application properties (i use yml):

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

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

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

这将导致以下映射:

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>

希望有帮助.

这篇关于spring:绕过contextPath为特定的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆