前幾天對接了一套第三方接口,這幾個第三方接口的請求地址一樣,請求參數和響應結果中有很多共同的字段,所以就想把這些字段都抽出來,通過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;
}
調用的時候報錯: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接口;
2、定義一個CustomizedConfiguration類,用於包裝GenericsFeignResultDecoder實例,用configuration屬性爲Feign指定自當前配置類。
@FeignClient(name = "demoFeign", url = "${config.demo.domain}", configuration = DemoFeign.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 FeignResultDecoder();
}
}
public class GenericsFeignResultDecoder implements Decoder {
@Override
public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
if (response.body() == null) {
throw new DecodeException(response.status(), "no data response");
}
Class returnType=((Method)((TypeVariableImpl)type).getGenericDeclaration()).getReturnType();
String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
return JSON.parseObject(bodyStr,returnType);
}
}
}
如果要爲當前Spring容器管理的所有Feign都指定這個解碼器,就把CustomizedConfiguration類挪到Feign接口外面,再加@Configuration,我這裏爲了方便就寫到Feign接口裏了;如果只是爲一個Feign Client指定自定義的解碼器,GenericsFeignResultDecoder就不要加Spring註解(不要被Spring管理)了,否則就成了全局的了。