在Feign接⼝中返回泛型类型——⾃定义Decoder
前⼏天对接了⼀套第三⽅接⼝,所有接⼝的请求地址⼀样,请求参数和响应结果中有很多共同的字段,所以就想把这些字段都抽出来,Feign定义的接⼝直接返回泛型类型。
Feign定义
@FeignClient(name ="demoFeign", url ="${config.demo.domain}")
public interface DemoFeign {
@PostMapping(value ="/open/post")
public<R extends BaseResponse, T extends BaseRequest> R invoke(@RequestBody T request);
}
请求参数⽗类 BaseRequest
@Data
public class BaseRequest{
private String requestId;
private String timeStamp;
private String method;
}
接⼝1的请求参数定义 Request01
@Data
public class Request01 extends BaseRequest{
private String merchantId;
}
接⼝2的请求参数定义 Request02
@Data
public class Request02 extends BaseRequest{
private String orderNo;
}
响应结果⽗类 BaseRequest
@Data
public class BaseResponse{
private String code;
private String message;
}
接⼝1的响应结果定义 Response01
@Data
public class Response01 extends BaseResponse{
private String merchantId;
private String merchantName;
}
接⼝2的响应结果定义 Response02
@Data
public class Response02 extends BaseResponse{
private String orderNo;
private String orderTime;
}
调⽤的时候报错:dec.DecodeException: type is not an instance of Class or ParameterizedType: R
at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:61)
at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:62)
at feign.optionals.OptionalDecoder.decode(OptionalDecoder.java:36)
at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:176)
at uteAndDecode(SynchronousMethodHandler.java:140)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
at com.sun.proxy.$Proxy129.invoke(Unknown Source)
原来是当接⼝返回类型定义成泛型时,Feign的解码器Decoder(Feign默认的解码器是SpringDecoder)在解析接⼝响应内容的时候,Type被解析成了TypeVariableImpl类型,导致反序列化
响应内容失败。
Feign的编码器和解码器是可插拔的,可以⾃定义⼀个Feign的解码器来解决这个问题。
1、定义⼀个 解析 返回类型为泛型 的 Feign接⼝ 的 解码器GenericsFeignResultDecoder,需要实现D
ecoder接⼝;
public class GenericsFeignResultDecoder implements Decoder {
private static NamedThreadLocal<Class> feignReturnTypeThreadLocal=new NamedThreadLocal<Class>("feignReturnTypeThreadLocal");
// 调⽤Feign的泛型接⼝前,先调⽤GenericsFeignResultDecoder.setReturnType()⽅法设置接⼝返回类型
public static void setReturnType(Class returnType){
feignReturnTypeThreadLocal.set(returnType);
}
// 重写Decode
@Override
public Object decode(Response response, Type type)throws IOException, DecodeException, FeignE
xception {
try{
if(response.body()==null){
throw new DecodeException(response.status(),"no data response");
}
Class ();
String bodyStr = String(response.body().asReader(Util.UTF_8));
return JSON.parseObject(bodyStr,returnType);
}catch(Exception e){
<("GenericsFeignResultDecoder.decode error", e);
}finally{
}
return null;
}
}
2、定义⼀个CustomizedConfiguration类,⽤于包装GenericsFeignResultDecoder实例,⽤configuration属性为Feign指定⾃当前配置类。
@FeignClient(name ="demoFeign", url ="${config.demo.domain}", configuration = CustomizedConfiguration.class)
public interface DemoFeign {
@PostMapping(value ="/open/post")
public<R extends BaseResponse, T extends BaseRequest> R invoke(@RequestBody T request);
public class CustomizedConfiguration{
@Bean
public Decoder feignDecoder(){
return new GenericsFeignResultDecoder();
}
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder(){
return Feign.builder();
}
}
}
如果要为当前Spring容器管理的所有Feign都指定这个解码器,就把CustomizedConfiguration类挪到Feign接⼝外⾯,再加
@Configuration,我这⾥为了⽅便就写到Feign接⼝⾥了;如果只是为⼀个Feign Client指定⾃定义的解码
器,GenericsFeignResultDecoder就不要加Spring注解(不要被Spring管理)了,否则就成了全局的了。
调⽤Feign的时候,需要先设置Feign接⼝返回的具体类型,这⾥通过ThreadLocal来传递Feign接⼝返回值的具体类型,在调⽤前把返回值类型放在ThreadLocal中,调⽤完再remove掉。
需要注意的是,⽤这种⽅法需要设置Hystrix的隔离策略为信号量隔离(默认为线程隔离),或者为当前FeignClient禁⽌Hystrix,上⾯的代码就为DemoFeign禁⽌了Hystrix。
调⽤Feign
decoder
@Service
public class DemoService{
@Autowired
private DemoFeign demoFeign;
public void function01(Request01 request){
GenericsFeignResultDecoder.setReturnType(Response01.class);
Response01 response=demoFeign.invoke(request);
// ……
}
public void function02(Request02 request){
GenericsFeignResultDecoder.setReturnType(Response02.class);
Response02 response=demoFeign.invoke(request);
/
/ ……
}
}
转载请注明出处——胡⽟洋《在Feign接⼝中返回泛型类型——⾃定义Decoder》blog.csdn/huyuyang6688/article/details/106926451

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。