Feign rest client,禁用对特定接口方法的解码
Feign rest client, disable decode on specific interface methods
通过学习曲线,遇到了这个场景:
鉴于90%的调用是JSON,在构建客户端时添加了GSON解码器。不过接口里面有些方法调用应该是支持raw的return不解码的
@RequestLine("GET /rest/myrawmethod")
String getRawMethod();
目前,由于 GSON 被添加为解码器,而不是 returning 原始字符串,它会尝试对其进行解码(它看起来确实像 JSON 内容,但我想绕过解码)。当不使用 GSON 解码器作为例外时,我似乎找不到一种简单的方法来禁用特定的接口方法。
谢谢!
看到一些对各种方法的引用,这似乎是目前最好的途径:
@RequestLine("GET /rest/myrawmethod")
feign.Response getRawMethod();
然后当你去解析响应时,使用像这样的东西:
feign.codec.Decoder dc = new Decoder.Default();
String strresponse = dc.decode(myfeignresponse, String.class); //wrapped with exception handling
在 REST 有效负载周围没有任何东西,只有方法调用的情况下进行原型制作的好方法……或者想要做一些更奇特的事情(比如使用 feign.Response 流方法)。
尝试像这样自定义 Decoder
:
class StringHandlingDecoder implements Decoder {
private final Decoder defaultDecoder;
StringHandlingDecoder(Decoder defaultDecoder) {
this.defaultDecoder = defaultDecoder;
}
@Override
public Object decode(Response response, Type type) throws IOException, FeignException {
if (type == String.class) {
return new StringDecoder().decode(response, type);
} else {
return this.defaultDecoder.decode(response, type);
}
}
}
然后像这样构建您的客户端:
Feign.builder()
.decoder(new StringHandlingDecoder(new GsonDecoder()))
.build();
通过学习曲线,遇到了这个场景:
鉴于90%的调用是JSON,在构建客户端时添加了GSON解码器。不过接口里面有些方法调用应该是支持raw的return不解码的
@RequestLine("GET /rest/myrawmethod")
String getRawMethod();
目前,由于 GSON 被添加为解码器,而不是 returning 原始字符串,它会尝试对其进行解码(它看起来确实像 JSON 内容,但我想绕过解码)。当不使用 GSON 解码器作为例外时,我似乎找不到一种简单的方法来禁用特定的接口方法。
谢谢!
看到一些对各种方法的引用,这似乎是目前最好的途径:
@RequestLine("GET /rest/myrawmethod")
feign.Response getRawMethod();
然后当你去解析响应时,使用像这样的东西:
feign.codec.Decoder dc = new Decoder.Default();
String strresponse = dc.decode(myfeignresponse, String.class); //wrapped with exception handling
在 REST 有效负载周围没有任何东西,只有方法调用的情况下进行原型制作的好方法……或者想要做一些更奇特的事情(比如使用 feign.Response 流方法)。
尝试像这样自定义 Decoder
:
class StringHandlingDecoder implements Decoder {
private final Decoder defaultDecoder;
StringHandlingDecoder(Decoder defaultDecoder) {
this.defaultDecoder = defaultDecoder;
}
@Override
public Object decode(Response response, Type type) throws IOException, FeignException {
if (type == String.class) {
return new StringDecoder().decode(response, type);
} else {
return this.defaultDecoder.decode(response, type);
}
}
}
然后像这样构建您的客户端:
Feign.builder()
.decoder(new StringHandlingDecoder(new GsonDecoder()))
.build();