Controller层通用返回类型之ResponseResult详解
JAVA开发大家都知道有经典三层,Controller, Service, Dao层.
Controller层是为了返回给用户查看的,因此返回体的定义是必不可少的。
废话不多说,直接上代码:
public class ResponseResult<T> implements Serializable {
private static final long serialVersionUID = 958295628567280402L;
/**
* 结果编码
*/
private int code;
/**
* 返回值
*/
@ConvDict(type = Const.DICT_DATATYPE_CLASS)
private T data;
/**
* 错误提示
*/
private String msg;
/**
* 默认响应成功
*/
public ResponseResult() {
code = ResultCode.SUCCESS;
}
/**
* 正常响应成功
*
* @param data 响应数据
*/
public ResponseResult(T data) {
this(ResultCode.SUCCESS, data, null);
}
/**
* 设置响应结果
*
* @param code 响应码
* @param data 响应数据
*/
public ResponseResult(int code, T data, String msg) {
this.code = code;
this.msg = msg;
this.data = data;
}
public ResponseResult(int code,String msg) {
this.code = code;
this.msg = msg;
}
/**
* @Method: success
* @Description: 默认执行成功
* @Params: []
* @History:
**/
public static <T> ResponseResult<T> success() {
return new ResponseResult<T>();
}
/**
* @Method: success
* @Description: 默认执行成功带结果
* @Params: [data]
* @History:
**/
public static <T> ResponseResult<T> success(T data) {
return new ResponseResult<T>(data);
}
/**
* @Method: fail
* @Description: 默认执行失败
* @Params: []
* @History:
**/
public static <T> ResponseResult<T> fail() {
return new ResponseResult<T>();
}
/**
* @Method: fail
* @Description: 默认执行失败带消息
* @Params: [msg]
* @History:
**/
public static <T> ResponseResult<T> fail(String msg) {
return new ResponseResult<T>(ErrorCodeEnums.SYSTEM_EXCEPTION.getCode(), msg);
}
public static <T> boolean isSuccess(ResponseResult<T> result) {
return 0 == result.getCode();
}
public static <T> boolean isFail(ResponseResult<T> result) {
return !isSuccess(result);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "ResponseResult{" +
"code=" + code +
", data=" + data +
", msg='" + msg + '\'' +
'}';
}
}
错误枚举类
public enum ErrorCodeEnums {
SYSTEM_EXCEPTION("SYSTEM_EXCEPTION", "系统异常", ResultCode.SYSTEM_ERROR),
INTERFACE_EXCEPTION("INTERFACE_EXCEPTION", "接口异常", ResultCode.SYSTEM_ERROR),
PARAM_VALIDATE_EXCEPTION("PARAM_VALIDATE_EXCEPTION", "参数校验异常", ResultCode.PARAM_VALIDATE_ERROR),
USER_NOT_LOGIN_EXCEPTION("USER_NOT_LOGIN_EXCEPTION", "用户未登录", ResultCode.NOT_LOGIN_ERROR),
USER_REPEAT_EXCEPTION("USER_REPEAT_EXCEPTION", "用户表存在重复用户信息", ResultCode.NOT_LOGIN_ERROR),
USER_NOT_LOGINNAME_EXCEPTION("USER_NOT_LOGINNAME_EXCEPTION", "登录账号不能为空",
EXCEL_EXPORT_EXCEPTION("EXCEL_EXPORT_EXCEPTION", "EXCEL导出异常", ResultCode.EXCEL_OPERATE_ERROR),
EXCEL_IMPORT_EXCEPTION("EXCEL_IMPORT_EXCEPTION", "EXCEL导入异常", ResultCode.EXCEL_OPERATE_ERROR),
MSG_ILLEGAL_ACCESS_EXCEPTION("MSG_ILLEGAL_ACCESS_EXCEPTION", "非法访问!", ResultCode.EXCEL_OPERATE_ERROR),
MSG_BEANS_EXCEPTION("MSG_BEANS_EXCEPTION", "bean转化异常!", ResultCode.EXCEL_OPERATE_ERROR),
MSG_PATH_EXCEPTION("MSG_PATH_EXCEPTION", "未找到路径异常!", ResultCode.EXCEL_OPERATE_ERROR), WORKPROJECT_BEGINTIMEAFTERENDTIME_EXCEPTION("WORKPLAN_BEGINTIMEAFTERENDTIME_EXCEPTION", "开工时间不能大于等于竣工时间", ResultCode.EXCEL_OPERATE_ERROR),
WORKPLAN_PHONE_EXCEPTION("WORKPLAN_PHONE_EXCEPTION", "手机号不能为空", ResultCode.EXCEL_OPERATE_ERROR),
WORKPLAN_PHONE_WITH_PARAM_EXCEPTION("WORKPLAN_PHONE_WITH_PARAM_EXCEPTION", "{0}手机号不能为空", ResultCode.EXCEL_OPERATE_ERROR),
WORKPLAN_SPECIALCHAR_EXCEPTION("WORKPLAN_SPECIALCHAR_EXCEPTION", "{0}必须为汉字/英文/数字,当前不合法值为:{1}", ResultCode.EXCEL_OPERATE_ERROR),
COMMON_PHONESIZE_EXCEPTION("COMMON_PHONESIZE_EXCEPTION", "手机号码长度须为11位", ResultCode.EXCEL_OPERATE_ERROR),
COMMON_PHONESIZE_WITH_PARAM_EXCEPTION("COMMON_PHONESIZE_WITH_PARAM_EXCEPTION", "{0}手机号码长度须为11位", ResultCode.EXCEL_OPERATE_ERROR);
/**
* 错误名字
*/
private String name;
/**
* 错误描述
*/
private String desc;
/**
* 结果编码
*/
private int code;
ErrorCodeEnums(String name, String desc, int code) {
this.name = name;
this.desc = desc;
this.code = code;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
public int getCode() {
return code;
}
}
结果编码:
public interface ResultCode {
/**
* 成功
*/
int SUCCESS = 0;
/**
* 系统异常
*/
int SYSTEM_ERROR = 1;
/**
* 参数校验失败
*/
int PARAM_VALIDATE_ERROR = 2;
/**
* 用户未登录
*/
int NOT_LOGIN_ERROR = 3;
/**
* 数据库操作失败
*/
int DB_OPERATE_ERROR = 4;
/**
* Excel操作失败
*/
int EXCEL_OPERATE_ERROR = 5;
/**
* Redis操作失败
*/
int REDIS_OPERATE_ERROR = 6;
}