Java 统一返回结果集封装

本文档介绍了一个名为ResultMsg的Java实体类,用于封装统一的返回结果集。ResultMsg包含状态码、返回信息和数据三个字段,并提供预定义的成功与错误静态实例,便于快速构建响应。此外,还提供了设置data和resultMsg的方法,方便在不同场景下定制返回信息。
摘要由CSDN通过智能技术生成

封装实体类

package com.commons.vo;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回结果集实体类
 *
 * @author liudean
 * @date 2021/8/3 20:31
 */
@Data
public class ResultMsg implements Serializable {
    //状态码
    private Integer resultCode;

    //返回信息
    private String resultMsg;

    //返回值,一般为成功后返回的数据
    private Object data;


    public ResultMsg() {}

    public ResultMsg(Integer resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }

    public ResultMsg(Integer resultCode, String resultMsg, Object data) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.data = data;
    }

    /**
     * 配合静态对象直接设置 data 参数
     * @param data
     * @return
     */
    public ResultMsg setNewData(Object data) {
        ResultMsg error = new ResultMsg();
        error.setResultCode(this.resultCode);
        error.setResultMsg(this.resultMsg);
        error.setData(data);
        return error;
    }

    /**
     * 配合静态对象直接设置 resultMsg 参数
     * @param resultMsg
     * @return
     */
    public ResultMsg setNewResultMsg(String resultMsg) {
        ResultMsg msg = new ResultMsg();
        msg.setResultCode(this.resultCode);
        msg.setResultMsg(resultMsg);
        msg.setData(this.data);
        return msg;
    }


    public static final ResultMsg SUCCESS = new ResultMsg(200, "成功");

    public static final ResultMsg INSERT_SUCCESS = new ResultMsg(200, "新增成功");

    public static final ResultMsg UPDATE_SUCCESS = new ResultMsg(200, "更新成功");

    public static final ResultMsg DELETE_SUCCESS = new ResultMsg(200, "删除成功");

    public static final ResultMsg SELECT_SUCCESS = new ResultMsg(200, "查询成功");

    public static final ResultMsg UPLOAD_SUCCESS = new ResultMsg(200, "上传成功");

    public static final ResultMsg DOWNLOAD_SUCCESS = new ResultMsg(200, "下载成功");

    public static final ResultMsg LOGIN_SUCCESS = new ResultMsg(200, "登陆成功");

    public static final ResultMsg LOGOUT_SUCCESS = new ResultMsg(200, "登出成功");

    public static final ResultMsg LOGIN_ERROR = new ResultMsg(201, "登陆错误");

    public static final ResultMsg LOGIN_EXPIRE = new ResultMsg(202, "登陆过期");

    public static final ResultMsg ACCESS_LIMITED = new ResultMsg(301, "访问受限");

    public static final ResultMsg ARGS_ERROR = new ResultMsg(501, "参数错误");

    public static final ResultMsg UNKOWN_ERROR = new ResultMsg(502, "系统异常");

    public static final ResultMsg INSERT_ERROR = new ResultMsg(503, "新增失败");

    public static final ResultMsg UPDATE_ERROR = new ResultMsg(504, "更新失败");

    public static final ResultMsg DELETE_ERROR = new ResultMsg(506, "删除失败");

    public static final ResultMsg SELECT_ERROR = new ResultMsg(507, "查询失败");

    public static final ResultMsg UPLOAD_ERROR = new ResultMsg(508, "上传失败");

    public static final ResultMsg DOWNLOAD_ERROR = new ResultMsg(509, "下载失败");

    public static final ResultMsg OTHER_SYSTEM_ERROR = new ResultMsg(510, "调用系统异常");

    public static final ResultMsg TIMOUT_ERROR = new ResultMsg(511, "调用超时");

}

调用

例如:
成功返回

return ResultMsg.SUCCESS;

成功带参返回

return ResultMsg.SUCCESS.setNewData(list);

错误返回并设定自定义错误信息

return ResultMsg.ARGS_ERROR.setNewResultMsg("缺少必要参数");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值