自定义结果类

  1. 自定义结果类
    参考:https://www.jianshu.com/p/398bf406f8e6
    参考:https://blog.csdn.net/zhang150114/article/details/90477002
    参考:https://zhuanlan.zhihu.com/p/414255238
    参考:https://blog.csdn.net/dedede001/article/details/128267515

  2. 示例代码

// ResponseResult类
@Data
@NoArgsConstructor
public class ResponseResult<T> implements Serializable {
//
private Integer code;
private String msg;
private T data;
// 成功-数据
public static <T> ResponseResult<T> ok(T data) {
return build(data, ResultEnum.SUCCESS);
}
// 成功--空参
public static <T> ResponseResult<T> ok() {
return ok(null);
}
// 成功--数据和提示信息
public static <T> ResponseResult<T> ok(String msg,T data) {
ResponseResult<T> result = ok(data);
result.setMsg(msg);
return result;
}
// 成功--提示信息
public static <T> ResponseResult<T> ok(String msg) {
ResponseResult<T> result = ok();
result.setMsg(msg);
return result;
}
// 失败--传枚举
public static <T> ResponseResult<T> fail(ResultEnum resultEnum) {
return build(null, resultEnum);
}
// 失败--什么也不传
public static <T> ResponseResult<T> fail() {
return build(null, ResultEnum.FAIL);
}
// 失败--传数据和枚举
public static <T> ResponseResult<T> fail(T data, ResultEnum resultEnum) {
return build(data, resultEnum);
}
// 构造数据--传所有变量
public static <T> ResponseResult<T> build(Integer code, String msg, T data) {
ResponseResult<T> result = build(data);
result.setMsg(msg);
result.setCode(code);
return result;
}
// 构造数据--只传数据
public static <T> ResponseResult<T> build(T data) {
ResponseResult<T> responseResult = new ResponseResult<>();
if (data != null) {
responseResult.setData(data);
}
return responseResult;
}
// 构造数据--传数据和枚举值
public static <T> ResponseResult<T> build(T data, ResultEnum resultEnum) {
ResponseResult<T> responseResult = build(data);
responseResult.setCode(resultEnum.getCode());
responseResult.setMsg(resultEnum.getMsg());
return responseResult;
}
}
// 枚举类
public enum AppHttpCodeEnum {
// 成功段固定为200
SUCCESS(200,"操作成功"),
// 登录段1~50
NEED_LOGIN(1,"需要登录后操作"),
LOGIN_PASSWORD_ERROR(2,"密码错误"),
// TOKEN50~100
TOKEN_INVALID(50,"无效的TOKEN"),
TOKEN_EXPIRE(51,"TOKEN已过期"),
TOKEN_REQUIRE(52,"TOKEN是必须的"),
// SIGN验签 100~120
SIGN_INVALID(100,"无效的SIGN"),
SIG_TIMEOUT(101,"SIGN已过期"),
// 参数错误 500~1000
PARAM_REQUIRE(500,"缺少参数"),
PARAM_INVALID(501,"无效参数"),
PARAM_IMAGE_FORMAT_ERROR(502,"图片格式有误"),
SERVER_ERROR(503,"服务器内部错误"),
// 数据错误 1000~2000
DATA_EXIST(1000,"数据已经存在"),
AP_USER_DATA_NOT_EXIST(1001,"ApUser数据不存在"),
DATA_NOT_EXIST(1002,"数据不存在"),
// 数据错误 3000~3500
NO_OPERATOR_AUTH(3000,"无权限操作"),
NEED_ADMIND(3001,"需要管理员权限"),
// 自媒体文章错误 3501~3600
MATERIASL_REFERENCE_FAIL(3501,"素材引用失效");
int code;
String errorMessage;
AppHttpCodeEnum(int code, String errorMessage){
this.code = code;
this.errorMessage = errorMessage;
}
public int getCode() {
return code;
}
public String getErrorMessage() {
return errorMessage;
}
}

posted on   朝朝暮Mu  阅读(24)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2022-04-11 laravel学习笔记之数据库配置
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示