美文网首页
【java】返回集合为null修改为空集合

【java】返回集合为null修改为空集合

作者: _王子_ | 来源:发表于2020-12-04 16:31 被阅读0次

查询接口有数据返回,是list结构

{
    "msg": "success",
    "code": 200,
    "data": {
        "total": 1,
        "data": [
            {
                "gmtModified": null,
                "gmtCreated": null,
                "groupId": 0,
                "modifier": "测试测试",
                "channel": 0,
                "description": null,
                "title": null,
                "totalCount": null,
                "recordNum": 0,
                "execRecordList": null,
                "caseType": null,
                "storyName": "需求名称5561709",
                "extra": null,
                "caseModuleId": 1,
                "id": null,
                "requirementId": "storyid_9540291",
                "productLineId": 0,
                "caseContent": "",
                "creator": "测试测试",
                "isDelete": null,
                "smkCaseId": null,
                "moduleNodeId": 0,
                "taskName": null,
                "caseContentMap": null,
                "taskId": null
            }
      ]
    }
}

当接口查询没有数据,返回data为null

{
    "msg": "success",
    "code": 200,
    "data": {
        "total": 0,
        "data": null
    }
}

不符合前端查询条件
查看代码结构

public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Number total = 0;
    private List<T> data = null;  // 默认返回null


    public PageResult() {
    }

修改默认返回内容 private List<T> data = Collections.emptyList();
Collections.emptyList();表示返回空list,只是查询已有数据,不需要新建空list

import java.util.Collections;
……

public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Number total = 0;
//    private List<T> data = null;  // 默认返回null
    private List<T> data = Collections.emptyList();   // 返回空list

    public PageResult() {
    }

测试结果

{
    "msg": "success",
    "code": 200,
    "data": {
        "total": 0,
        "data": []
    }
}

相关文章

网友评论

      本文标题:【java】返回集合为null修改为空集合

      本文链接:https://www.haomeiwen.com/subject/vyriwktx.html