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

public class DistributeServiceResultBean implements Serializable {

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

写法1:

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

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

 

写法2:

ParameterizedTypeReference> typeRef = new ParameterizedTypeReference>() {};

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

 

你可能感兴趣的:(java,EE,springboot)