Java映射 转换post response T data

2024-10-09 11:04:05 浏览数 (1)

Java映射 转换post response data

接上篇

Java泛型对象在http请求和响应对象中的封装

https://cloud.tencent.com/developer/article/2455773

demo1

代码语言:javascript复制
public class ResponseBase<T> {
    private String resCode;
    private String resMsg;
//    private List<T> data;
    private T data;   //实际接收的是String字符串


    public String getResCode() {
        return resCode;
    }

    public void setResCode(String resCode) {
        this.resCode = resCode;
    }

    public String getResMsg() {
        return resMsg;
    }

    public void setResMsg(String resMsg) {
        this.resMsg = resMsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
    
}

public class JsonListTest {
    public static void main(String[] args) {
        String myRemarkJson = "Json串,data可以是列表的json串";

        List<MyAppWorkOrderResponseVO> list = new ArrayList<>();

//        ResponseBase<String> responseVO = JSON.parseObject(myRemarkJson, ResponseBase.class);
        //同上面的 默认是String
        ResponseBase responseVO = JSON.parseObject(myRemarkJson, ResponseBase.class);
        //如果写成如下是错误的
        //ResponseBase<MyAppWorkOrderResponseVO> responseVO = JSON.parseObject(myRemarkJson, ResponseBase.class);
        
        System.out.println("getRemarkInfo responseData="   GsonUtils.toJson(responseVO));
        if(responseVO != null && "000000".equals(responseVO.getResCode()) && responseVO.getData() != null) {
//                //去掉   syntax error, expect {, actual [, pos 0, fastjson-version 1.2.83
            System.out.println("sss="   responseVO.getData().toString());
            list = JSON.parseArray(responseVO.getData().toString(), MyAppWorkOrderResponseVO.class);
        }
        System.out.println("getRemarkInfo list="   GsonUtils.toJson(list));

    }
}

demo2

代码语言:javascript复制
public class MyPartnerBaseResponse<T> extends PartnerBaseResponse {
    /**
     *状态码表0成功,其他失败
     */
    private Integer returncode;
    /**
     *业务操作码 成功”success”
     */
    private String operate;
    /**
     *返回结果消息
     */
    private String message;
    /**
     * 返回结果数据,详情json
     */
    private T result;

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public Integer getReturncode() {
        return returncode;
    }

    public void setReturncode(Integer returncode) {
        this.returncode = returncode;
    }

    public String getOperate() {
        return operate;
    }

    public void setOperate(String operate) {
        this.operate = operate;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}


        //默认T是接收的是字符串
        MyPartnerBaseResponse baseResVo = JSON.parseObject(resultJsonData, MyPartnerBaseResponse.class);
        if(baseResVo.getResult() != null) {
            response = JSON.parseObject(baseResVo.getResult().toString(), PiccHangzhouAuthorizationQueryResponse.class);
            baseResVo.setResult(response);
        }

0 人点赞