禁用 PathVariables 的假编码
Disable feign encoding of PathVariables
我有以下 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 有问题跟踪:
猜猜它会在完成后由 spring-cloud-feign
实施。
同时,我针对此问题的解决方法是创建 RequestInterceptor
并将 %3A
替换为 :
public class MyRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
template.uri(template.path().replaceAll("%3A", ":"));
}
}
并使用此 requestInterceptor
在 feignConfig
中构建您的 feignClient
:
@Bean
public Feign.Builder tcsClientBuilder() {
return Feign.builder().requestInterceptor(new MyRequestInterceptor());
}
这对我来说很有效:
//ICustomFeignClient = your Feign client interface
Feign.builder()
.client(new OkHttpClient())
.encoder(new GsonEncoder())
.decoder(new GsonDecoder())
.logger(new Slf4jLogger(builderClass))
.logLevel(Logger.Level.FULL)
.requestInterceptor(new CustomFeignRequestInterceptor())
.target(ICustomFeignClient, baseUrl);
// custom interceptor class below
class CustomFeignRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
// you can replace the string you don't want from here
requestTemplate.uri(
requestTemplate.request()
.url()
.replaceAll("%3D", "=")
.replaceAll("%26", "&")
);
}
}
我有以下 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 有问题跟踪:
猜猜它会在完成后由 spring-cloud-feign
实施。
同时,我针对此问题的解决方法是创建 RequestInterceptor
并将 %3A
替换为 :
public class MyRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
template.uri(template.path().replaceAll("%3A", ":"));
}
}
并使用此 requestInterceptor
在 feignConfig
中构建您的 feignClient
:
@Bean
public Feign.Builder tcsClientBuilder() {
return Feign.builder().requestInterceptor(new MyRequestInterceptor());
}
这对我来说很有效:
//ICustomFeignClient = your Feign client interface
Feign.builder()
.client(new OkHttpClient())
.encoder(new GsonEncoder())
.decoder(new GsonDecoder())
.logger(new Slf4jLogger(builderClass))
.logLevel(Logger.Level.FULL)
.requestInterceptor(new CustomFeignRequestInterceptor())
.target(ICustomFeignClient, baseUrl);
// custom interceptor class below
class CustomFeignRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
// you can replace the string you don't want from here
requestTemplate.uri(
requestTemplate.request()
.url()
.replaceAll("%3D", "=")
.replaceAll("%26", "&")
);
}
}