禁用路径变量的假装编码



我有以下假客户端:

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", ":"));
}
}

并使用此requestInterceptorfeignConfig中构建您的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", "&")
);
}
}

相关内容

  • 没有找到相关文章

最新更新