1. 程式人生 > >在feign介面中返回泛型類

在feign介面中返回泛型類

在feign介面中返回泛型類時,由於java的泛型機制,在例項化之前無法得到具體的型別 ,因此,雖然服務提供方返回的是具體例項的資料,但是在客戶端decode時,無法轉化為具體的類。
例如
@RequestMapping("getGoodByCode")  
<T extends BaseEntity> ObjectResponse<T> geByCode(@RequestParam("code") String code,@RequestParam("type") String type);
1
2
此介面 通過商品code和型別獲取商品類,此時我們只能接收到只能是ObjectResponse<BaseEntity> 即使服務方返回的是 ObjectResponse<Goods>。

解決辦法
服務提供方返回泛型型別的全限定名 如com.csdn.product.Goods 可放在header中
客戶端繼承Decoder 介面,重寫decode方法。
得到類名後和response資料.
通過反射例項化Goods,得到Goods 的Class類
通過反序列化,得到Goods 類例項,
然後set Response.setModel(goods )
Decoder
public class CustomDecoder implements Decoder {
    //泛型的classType ,,decode 方法進行手動解析.
    public final static String  genericsHeader = "generics-header";

    private Decoder decoder;
    public CustomDecoder(Decoder decoder) {
        this.decoder = decoder;
    }
    @Override
    public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
        Object returnObject = null;

        if (isParameterizeHttpEntity(type)) {
            type = ((ParameterizedType) type).getActualTypeArguments()[0];
            Object decodedObject = decoder.decode(response,type);

            returnObject =  createResponse(decodedObject, response);
        }
        else if (isHttpEntity(type)) {
            returnObject =  createResponse(null, response);
        }
        else {
            returnObject =  decoder.decode(response, type);
        }
   //以上是原預設實現,複製過來,為了拿到returnObject 
        if(returnObject !=null) {
            Map<String, Collection<String>> map = response.headers();
            if (returnObject instanceof ObjectResponse) {
                Collection<String> list  =  map.get(genericsHeader);
                if(list!=null){
                    Object object = ReflectUtil.newInstance(((LinkedList)list).get(0).toString());
                    String body = IoUtil.read(response.body().asInputStream(),"UTF-8");
                    ObjectMapper objectMapper  = new ObjectMapper();
                    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
                    JsonNode jsonNode = objectMapper.readTree(body);
                    if(!jsonNode.get("success").booleanValue()){
                        return returnObject;
                    }
                    //拿出result ,例項化物件(genericsHeader 指定的型別),然後重新set
                    String result = jsonNode.get("model").toString();
                    ((ObjectResponse) returnObject).setModel(objectMapper.readValue(result,object.getClass()));
                 
                }

            }
        }
//        log.info("計算耗時:{}",System.currentTimeMillis()-start);

        return  returnObject;
    }


    private boolean isParameterizeHttpEntity(Type type) {
        if (type instanceof ParameterizedType) {
            return isHttpEntity(((ParameterizedType) type).getRawType());
        }
        return false;
    }

    private boolean isHttpEntity(Type type) {
        if (type instanceof Class) {
            Class c = (Class) type;
            return HttpEntity.class.isAssignableFrom(c);
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    private <T> ResponseEntity<T> createResponse(Object instance, Response response) {

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        for (String key : response.headers().keySet()) {
            headers.put(key, new LinkedList<>(response.headers().get(key)));
        }

        return new ResponseEntity<>((T) instance, headers, HttpStatus.valueOf(response
                .status()));
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
ObjectResponse
public class ObjectResponse<T> {

    private boolean success;

    private String msg;

    private T model;

    public boolean isSuccess() {
        return success;
    }

    public ObjectResponse<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public ObjectResponse<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getModel() {
        return model;
    }

    public ObjectResponse<T> setModel(T model) {
        this.model = model;
        return this;
    }
}