java - 禁用 PathVariables 的假编码

标签 java spring encoding spring-cloud-feign path-variables

我有以下 Feign 客户端:

public interface MyServiceClient {
    @RequestMapping(method = RequestMethod.GET, value = "/item/{itemKey}")
    Item getItem (@PathVariable("itemKey") String itemKey);
}
项目可以包含特殊字符,如 :::哪些正在被编码。
请求 URL 变成这样:
  • https://myservice.com/item/a%3Ab%3A%3Ac

  • 而不是:
  • https://myservice.com/item/a:b::c

  • 谁能帮助我了解我们如何解决这个问题?

    最佳答案

    OpenFeign 有一个问题跟踪:

  • https://github.com/OpenFeign/feign/issues/1190

  • 估计会由spring-cloud-feign实现一旦完成。
    同时,我对这个问题的解决方法是创建一个 RequestInterceptor并替换 %3A:
    public class MyRequestInterceptor implements RequestInterceptor {
        @Override
        public void apply(RequestTemplate template) {
            template.uri(template.path().replaceAll("%3A", ":"));
        }
    }
    
    并使用此 requestInterceptor建立您的 feignClientfeignConfig :
    @Bean
    public Feign.Builder tcsClientBuilder() {
        return Feign.builder().requestInterceptor(new MyRequestInterceptor());
    }
    

    关于java - 禁用 PathVariables 的假编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61830167/

    相关文章:

    java - 下载不同编码的在线文本

    java - 单个 CardView 中的多个图像

    java - 如何使用 ASM 将 boolean 字段替换为常量值

    java - Python 到 Java 加密/解密,确保密码匹配?

    java - ANTLR4 -> 检查文字是 STRING_LITERAL 还是 NUMBER_LITERAL

    Spring事务传播REQUIRED,REQUIRES_NEW

    spring - Spring Boot 安全性的基本 URI

    spring - 延迟加载有效,但不应该

    python - 修复混合编码的 csv 中的编码错误

    具有不同编码的 Rd 文件