Restemplate多级泛型反序列化问题解决

在项目开发过程中碰到restemplate.exchange()方法反序列化失败,因为接收接口返回的数据是一个复杂的dto,该dto中存在泛型的属性。期待是转成 xxxDto,可是得到的确实LinkedMap,并且抛出了转换类型错误。

RestTemplate的多级泛型和消息转换器

RestTemplate 反序列化带泛型的返回值

ParameterizedTypeReference官网

解决

首先看看 exchange源码

public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
        Type type = responseType.getType();
        RequestCallback requestCallback = this.httpEntityCallback(requestEntity, type);
        ResponseExtractor<ResponseEntity<T>> responseExtractor = this.responseEntityExtractor(type);
        return (ResponseEntity)nonNull(this.execute(url, method, requestCallback, responseExtractor, uriVariables));
    }

通过exchange()源码可以看出需序列化的返回类型也是参数化类型(ParameterizedTypeReference),看来exchange()支持多级泛型的dto,但只能带一个参数(List<T>)

解决工具

ParameterizedTypeReference<T> 源码

public abstract class ParameterizedTypeReference<T> {
    private final Type type;

    protected ParameterizedTypeReference() {
        Class<?> parameterizedTypeReferenceSubclass = findParameterizedTypeReferenceSubclass(this.getClass());
        Type type = parameterizedTypeReferenceSubclass.getGenericSuperclass();
        //isInstanceOf(Class clazz, Object obj) / isInstanceOf(Class type, Object obj, String message)  如果 obj 不能被正确造型为 clazz 指定的类将抛出异常;
        Assert.isInstanceOf(ParameterizedType.class, type, "Type must be a parameterized type");
        ParameterizedType parameterizedType = (ParameterizedType)type;
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        //复杂dto(含有泛型) 只允许有一个参数,例:List<T>
        Assert.isTrue(actualTypeArguments.length == 1, "Number of type arguments must be 1");
        this.type = actualTypeArguments[0];
    }

    private ParameterizedTypeReference(Type type) {
        this.type = type;
    }

    public Type getType() {
        return this.type;
    }

    public boolean equals(@Nullable Object other) {
        return this == other || other instanceof ParameterizedTypeReference && this.type.equals(((ParameterizedTypeReference)other).type);
    }

    public int hashCode() {
        return this.type.hashCode();
    }

    public String toString() {
        return "ParameterizedTypeReference<" + this.type + ">";
    }

    public static <T> ParameterizedTypeReference<T> forType(Type type) {
        return new ParameterizedTypeReference<T>(type) {
        };
    }

    //查找 ParameterizedTypeReference 类的子类
    private static Class<?> findParameterizedTypeReferenceSubclass(Class<?> child) {
        Class<?> parent = child.getSuperclass();
        if (Object.class == parent) {
            throw new IllegalStateException("Expected ParameterizedTypeReference superclass");
        } else {
            return ParameterizedTypeReference.class == parent ? child : findParameterizedTypeReferenceSubclass(parent);
        }
    }
}

ParameterizedType(参数化类型)是什么请参考:ParameterizedType详解

实现

xxxDto

public class xxxDto<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private String statusCode;
    private String  statusMsg;
    private T data;

    public boolean isSuccess(){
        return "000000".equals(statusCode);
    }

}

restemplate 调用

xxxDto<xxxDto> dto = networkProxy.request(NetworkProxy.Target.XXX, HttpMethod.POST, httpEntity,
                new ParameterizedTypeReference<xxxDto<xxxDto>>() {
                }, new HashMap<>());
public <T> T request(Target target, HttpMethod httpMethod, HttpEntity httpEntity, ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables) {
        log.info(proxyAddress + target.getUrl());
        return restTemplate.exchange(proxyAddress + target.getUrl(), httpMethod, httpEntity, responseType, uriVariables).getBody();
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值