阿超
>
open-feign自定义反序列化decoder
人受谏,则圣。木受绳,则直。金就砺,则利。——孔子
主要是解决map
类型擦除的问题,GlobalResponse
继承了Map
代码如下:
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
| import cn.hutool.core.util.TypeUtil; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import feign.RequestTemplate; import feign.Response; import feign.Util; import feign.codec.DecodeException; import feign.codec.Decoder; import feign.codec.EncodeException; import feign.codec.Encoder; import jakarta.annotation.Resource; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Bean;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type;
@Bean public Decoder feignDecoder() { return (response, type) -> { if (response.status() == 404 || response.status() == 204) { return Util.emptyValueOf(type); } if (response.body() == null) { return null; } if (byte[].class.equals(type)) { return Util.toByteArray(response.body().asInputStream()); } Response.Body body = response.body(); if (String.class.equals(type)) { return Util.toString(body.asReader(Util.UTF_8)); } if (GlobalResponse.class.equals(type)) { return objectMapper.readValue(body.asInputStream(), GlobalResponse.class); } if (!(type instanceof ParameterizedType)) { throw notSupportDecode(response, type); } ParameterizedType parameterizedType = TypeUtil.toParameterizedType(type); if (!(GlobalResponse.class.equals(parameterizedType.getRawType()))) { return objectMapper.readValue(body.asInputStream(), new TypeReference<>() { @Override public Type getType() { return type; } }); } JsonNode jsonNode = objectMapper.readTree(body.asInputStream()); GlobalResponsese<?> res = objectMapper.treeToValue(jsonNoGlobalResponseonse.class); JsonNode dataNode = jsonNodGlobalResponsesponse.DATA); if (dataNode.isNull()) { return res; } Object data = objectMapper.readValue(dataNode.toString(), new TypeReference<>() { @Override public Type getType() { return TypeUtil.getTypeArgument(type); } }); res.setData(data); return res; }; }
|