当前位置: 首页 > news >正文

自定义通用返回对象

目的:给返回对象补充一些信息,告诉前端这个请求在业务层面上是成功还是失败,以及具体的描述信息。

我们需要自定义错误码(因为前端的HTTP状态码默认的值比较少)和正常错误返回类。

ErrorCode :

package com.heo.ezyuserbackend.common;public enum ErrorCode {SUCCESS(0, "ok", ""),PARAMS_ERROR(40000, "请求参数错误", ""),NULL_ERROR(40001, "请求参数为空", ""),NOT_LOGIN(40100, "未登录", ""),NO_AUTH(40101, "无权限", ""),SYSTEM_ERROR(50000,"系统内部异常","");private final int code;/*** 状态码信息*/private  final String message;public int getCode() {return code;}public String getMessage() {return message;}public String getDescription() {return description;}/*** 状态码描述(详情)*/private final String description;ErrorCode(int code, String message, String description) {this.code = code;this.message = message;this.description = description;}
}

BaseResponse:

package com.heo.ezyuserbackend.common;import lombok.Data;import java.io.Serializable;/*** 通用返回类** @param <T>*/
@Data
public class BaseResponse<T> implements Serializable {private int code;private T data;private String message;private String description;public BaseResponse(int code, T data, String message, String description) {this.code = code;this.data = data;this.message = message;this.description = description;}public BaseResponse(int code, T data,String message) {this(code, data, message,"");}public BaseResponse(int code, T data) {this(code, data, "", "");}public BaseResponse(ErrorCode errorCode) {this(errorCode.getCode(), null, errorCode.getMessage(), errorCode.getDescription());}
}

ResultUtil :

package com.heo.ezyuserbackend.common;import com.fasterxml.jackson.databind.ser.Serializers;/*** 返回工具类*/
public class ResultUtil {/*** 成功** @param data* @param <T>* @return*/public static <T> BaseResponse<T> success(T data) {return new BaseResponse<>(0, data, "ok");}/*** 失败** @param errorCode* @return*/public static <T> BaseResponse<T> error(ErrorCode errorCode) {return new BaseResponse<>(errorCode);}/*** 失败** @param code* @param message* @param description* @param <T>* @return*/public static <T> BaseResponse<T> error(int code, String message, String description) {return new BaseResponse<>(code, null, message, description);}/*** 失败** @param errorCode* @param message* @param description* @param <T>* @return*/public static <T> BaseResponse<T> error(ErrorCode errorCode, String message, String description) {return new BaseResponse<>(errorCode.getCode(), null, message, description);}/*** 失败** @param errorCode* @param description* @return*/public static BaseResponse error(ErrorCode errorCode, String description) {return new BaseResponse<>(errorCode.getCode(), errorCode.getMessage(), description);}
}
http://www.lryc.cn/news/286975.html

相关文章:

  • 从0开始python学习-51.pytest之接口加密封装
  • c++的命名空间
  • 阿富汗塔利班兴起时的比赛代码3475:练85.3 删数问题(Noip1994)
  • 大数据平台红蓝对抗 - 磨利刃,淬精兵!
  • 【2024-01-22】某极验3流程分析-滑块验证码
  • Laya2.13.3接入FGUI
  • 短视频账号矩阵系统+无人直播系统源码技术开发
  • C语言或C++通过IShellLinkA创建或解析lnk快捷方式(使用char字符数组)
  • Spring源码学习-Spring流程概述(一)
  • Figma怎么设置中文,Figma有中文版吗?
  • 智慧文旅一机游:科技与文化的完美结合,引领智慧文旅新潮流,智慧旅游未来已来
  • 多维时序 | Matlab实现CNN-LSTM-Mutilhead-Attention卷积长短期记忆神经网络融合多头注意力机制多变量时间序列预测
  • 软件工程实验报告(完整)
  • Java零基础学习20:集合的练习
  • 【latex】在Overleaf的IEEE会议模板中,快速插入参考文献
  • java反射之Field用法(获取对象的字段名和属性值)
  • Java Web(三)--CSS
  • 天津大数据培训班推荐,数据分析过程的常见错误
  • 【笔记】Helm-3 主题-17 弃用的Kubernetes API
  • 麒麟系统—— openKylin 安装 java
  • HTML学习笔记——07:其他嵌入技术
  • 【UE】在控件蓝图中通过时间轴控制材质参数变化
  • linux C语言socket函数send
  • Django(八)
  • 上海计算机学会12月月赛 丙组题解
  • nextjs中beforePopState使用
  • 【并发编程】活锁
  • CSMM和CMMI之间有什么区别?
  • 企业面临的典型网络安全风险及其防范策略
  • JavaScript进阶:WebAPIs重点知识整理1