当前位置:网站首页>全局异常处理
全局异常处理
2022-06-22 00:57:00 【YLi_Jing】
全局异常处理
1、响应结果集封装
public class ResponseResult<T> implements Serializable {
private Integer code;
private String msg;
private T data;
public ResponseResult() {
this.code = AppHttpCodeEnum.SUCCESS.getCode();
this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
}
public ResponseResult(Integer code, T data) {
this.code = code;
this.data = data;
}
public ResponseResult(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public ResponseResult(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public static ResponseResult errorResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.error(code, msg);
}
public static ResponseResult okResult() {
ResponseResult result = new ResponseResult();
return result;
}
public static ResponseResult okResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.ok(code, null, msg);
}
public static ResponseResult okResult(Object data) {
ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
if (data != null) {
result.setData(data);
}
return result;
}
public static ResponseResult errorResult(AppHttpCodeEnum enums) {
return setAppHttpCodeEnum(enums, enums.getMsg());
}
public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg) {
return setAppHttpCodeEnum(enums, msg);
}
public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums) {
return okResult(enums.getCode(), enums.getMsg());
}
private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg) {
return okResult(enums.getCode(), msg);
}
public ResponseResult<?> error(Integer code, String msg) {
this.code = code;
this.msg = msg;
return this;
}
public ResponseResult<?> ok(Integer code, T data) {
this.code = code;
this.data = data;
return this;
}
public ResponseResult<?> ok(Integer code, T data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
return this;
}
public ResponseResult<?> ok(T data) {
this.data = data;
return this;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
2、异常响应状态枚举类
public enum AppHttpCodeEnum {
/** * 操作成功 */
SUCCESS(200,"操作成功"),
SYSTEM_ERROR(504,"系统未知错误"),
LOGIN_ERROR(505,"除数不能为0");
final int code;
final String msg;
AppHttpCodeEnum(int code, String errorMessage){
this.code = code;
this.msg = errorMessage;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
3、自定义全局异常类
public class SystemException extends RuntimeException{
private int code;
private String msg;
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public SystemException(AppHttpCodeEnum httpCodeEnum) {
super(httpCodeEnum.getMsg());
this.code = httpCodeEnum.getCode();
this.msg = httpCodeEnum.getMsg();
}
}
4、配置全局异常处理
/** * 全局异常处理 * @author YLi_Jing */
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(SystemException.class)
public ResponseResult systemExceptionHandler(SystemException e){
//从异常对象中获取提示信息封装返回
return ResponseResult.errorResult(e.getCode(),e.getMsg());
}
@ExceptionHandler(Exception.class)
public ResponseResult exceptionHandler(Exception e){
//从异常对象中获取提示信息封装返回
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),e.getMessage());
}
}
边栏推荐
- Panic: permission denied problems encountered when using gomonkey mock functions and methods and Solutions
- The use of PowerShell functions in mathematical expressions
- Install tensorflow and transformer on Orange Pie orangepi4b
- 求一个防关联检测工具,浏览器指纹在线检测
- Example and description of lvgl Demo 1
- 云堡垒机分布式集群部署优缺点简单说明
- 03 fastjson resolving circular references
- Evc4 program cannot run on the emulator
- 记录webscraper的使用过程
- 3分钟,带你玩转聊天机器人自动化【顶级模板】
猜你喜欢

站在数字化风口,工装企业如何“飞起来”

Huawei cloud releases desktop ide codearts

Using SSM framework to realize user login

依靠可信AI的鲁棒性有效识别深度伪造,帮助银行对抗身份欺诈

PHP admin deployment - resolve all errors

Dynamic programming-01 backpack, partition, etc. and subset, weight of the last stone

Brief introduction to jpom: simple and light low intrusive online construction, automatic deployment, daily operation and maintenance, and project monitoring software
![3 minutes, take you to play with chat robot automation [top template]](/img/71/4d848b46a52b71a351a086db248a95.png)
3 minutes, take you to play with chat robot automation [top template]

Handwriting database connection pool

点云配准--4PCS原理与应用
随机推荐
SQL operation: with expression and its application
有没有亚马逊跨境电商适合用的浏览器排名
使用 gomonkey 遇到非 debug 模式执行失败的问题及解决方法
Using SSM framework to realize user login
【ÑÖÏ模拟赛】花萎(矩阵加速,循环卷积,高斯消元)
容器云是什么意思?与堡垒机有什么区别?
Planification dynamique - 01 sac à dos, partitions et sous - ensembles, poids de la dernière pierre
刚学了一个炫酷3D三棱锥立体特效,快来看看
带你区分几种并行
Judge whether the string type is empty and whether the list set is empty
Is there a browser ranking suitable for Amazon cross-border E-commerce
Sending webhook of message queue to realize cross application asynchronous callback
How to remove duplication in left join from a simple example
Want to join a big factory? Reading this article may help you
Interview question catalog collection
站在数字化风口,工装企业如何“飞起来”
Redis implements distributed locks
[project construction] cmake create release and debug projects
動態規劃-01背包,分割等和子集,最後一塊石頭的重量
杨冰:OceanBase助力数字化转型,原生分布式数据库成核心系统首选