RestTemplate提交表单数据, 返回数据泛型包含泛型(两层泛型)

public class DistributeServiceResultBean<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;
}

写法1:

MultiValueMap<String, String> headers = new MultiValueMap<String, String>();
        HttpEntity<GetDistributeRuleParam> httpEntity = new HttpEntity(new GetDistributeRuleParam(), headers);
        ResponseEntity<DistributeServiceResultBean> responseEntity=restTemplate.postForEntity(getDistributeUrl,httpEntity,DistributeServiceResultBean.class);
        DistributeServiceResultBean<DistributeRuleInfo> distributeResultVO=responseEntity.getBody();

//此时,distributeResultVO里的data返回是一个map对象,而不是想要的泛型对象

 

写法2:

ParameterizedTypeReference<DistributeServiceResultBean<DistributeRuleInfo>> typeRef = new ParameterizedTypeReference<DistributeServiceResultBean<DistributeRuleInfo>>() {};

        ResponseEntity<DistributeServiceResultBean<DistributeRuleInfo>> responseEntity2 = restTemplate.exchange(getDistributeUrl, HttpMethod.POST, httpEntity, typeRef);
        DistributeServiceResultBean<DistributeRuleInfo> resultVO = responseEntity2.getBody();
//此时 resultVO 里面的data就是DistributeRuleInfo对象

 


版权声明:本文为klx502原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。