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;
}
调用的时候报错:feign.codec.DecodeException: type is not an instance of Class or ParameterizedType: R
feign.codec.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 feign.SynchronousMethodHandler.executeAndDecode(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,需要实现Decoder接口;
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, FeignException {
try{
if (response.body() == null) {
throw new DecodeException(response.status(), "no data response");
}
Class returnType=feignReturnTypeThreadLocal.get();
String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
return JSON.parseObject(bodyStr,returnType);
} catch (Exception e) {
log.error("GenericsFeignResultDecoder.decode error", e);
}finally {
feignReturnTypeThreadLocal.remove();
}
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
@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);
// ……
}
}