restTemplate 接收泛型处理

案例背景,

有的时候,restTemplate 响应的东西,是这样的

{

    "code": "9003",

    "msg": "参数错误",

    "timestamp": 1688970207513,

    "data": {

       

    }

}

 其中的data,不固定,我们不可能每一个不固定的都写一个实体类,或者直接用Object,

这里就可以使用

@Data
public class ResponseBasics2<T> {
    private String msg;
    private String code;
    private String timestamp;
    private T data;
}

 使用的时候,
 

        ResponseEntity<JSONObject> jsonpObjectResponseEntity2 = restTemplate.postForEntity(
                url, basics, JSONObject.class);


JSONObject body =jsonpObjectResponseEntity2.getBody();
        ResponseBasics2<Coupons>  response = JSON.parseObject(body.toString(), new TypeReference< ResponseBasics2<Coupons>>(){});

猜你喜欢

转载自blog.csdn.net/u013833472/article/details/131638455
今日推荐