封装实体类
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("缺少必要参数");