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

SpringBoot全局异常详解

一、前言

在日常的开发工作中,项目在运行过程中多多少少是避免不了报错的,对于报错信息肯定不可以把全部信息都抛给客户端去显示,这里就需要我们对常见的七种异常情况统一进行处理,让整个项目更加优雅。

二、全局异常介绍

这次博客的主角就是 @RestControllerAdvice 这个注解,这个一个组合注解由 @ControllerAdvice@ResponseBody 组成,@RestControllerAdvice 会帮助我们把信息转成json格式返回。

在全局异常处理类只需要在类上标注 @RestControllerAdvice ,并在处理相应异常的方法上使用 @ExceptionHandler 注解,写明处理哪个异常即可。

注:异常的拦截有顺序,子类异常会优先匹配子类异常处理器。

三、常用类封装

HttpStatus状态码常量类

这边定义了目前常见的响应的状态码


/*** 返回状态码**/
public class HttpStatus {/*** 操作成功*/public static final int SUCCESS = 200;/*** 对象创建成功*/public static final int CREATED = 201;/*** 请求已经被接受*/public static final int ACCEPTED = 202;/*** 操作已经执行成功,但是没有返回数据*/public static final int NO_CONTENT = 204;/*** 资源已被移除*/public static final int MOVED_PERM = 301;/*** 重定向*/public static final int SEE_OTHER = 303;/*** 资源没有被修改*/public static final int NOT_MODIFIED = 304;/*** 参数列表错误(缺少,格式不匹配)*/public static final int BAD_REQUEST = 400;/*** 未授权*/public static final int UNAUTHORIZED = 401;/*** 访问受限,授权过期*/public static final int FORBIDDEN = 403;/*** 资源,服务未找到*/public static final int NOT_FOUND = 404;/*** 不允许的http方法*/public static final int BAD_METHOD = 405;/*** 资源冲突,或者资源被锁*/public static final int CONFLICT = 409;/*** 不支持的数据,媒体类型*/public static final int UNSUPPORTED_TYPE = 415;/*** 系统内部错误*/public static final int ERROR = 500;/*** 接口未实现*/public static final int NOT_IMPLEMENTED = 501;/*** 系统警告消息*/public static final int WARN = 601;
}

AjaxResult统一封装返回的结果类

import java.util.HashMap;
import java.util.Objects;
/*** 操作消息提醒**/
public class AjaxResult extends HashMap<String, Object> {private static final long serialVersionUID = 1L;/*** 状态码*/public static final String CODE_TAG = "code";/*** 返回内容*/public static final String MSG_TAG = "msg";/*** 数据对象*/public static final String DATA_TAG = "data";/*** 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。*/public AjaxResult() {}/*** 初始化一个新创建的 AjaxResult 对象** @param code 状态码* @param msg  返回内容*/public AjaxResult(int code, String msg) {super.put(CODE_TAG, code);super.put(MSG_TAG, msg);}/*** 初始化一个新创建的 AjaxResult 对象** @param code 状态码* @param msg  返回内容* @param data 数据对象*/public AjaxResult(int code, String msg, Object data) {super.put(CODE_TAG, code);super.put(MSG_TAG, msg);if (data != null) {super.put(DATA_TAG, data);}}/*** 返回成功消息** @return 成功消息*/public static AjaxResult success() {return AjaxResult.success("操作成功");}/*** 返回成功数据** @return 成功消息*/public static AjaxResult success(Object data) {return AjaxResult.success("操作成功", data);}/*** 返回成功消息** @param msg 返回内容* @return 成功消息*/public static AjaxResult success(String msg) {return AjaxResult.success(msg, null);}/*** 返回成功消息** @param msg  返回内容* @param data 数据对象* @return 成功消息*/public static AjaxResult success(String msg, Object data) {return new AjaxResult(HttpStatus.SUCCESS, msg, data);}/*** 返回警告消息** @param msg 返回内容* @return 警告消息*/public static AjaxResult warn(String msg) {return AjaxResult.warn(msg, null);}/*** 返回警告消息** @param msg  返回内容* @param data 数据对象* @return 警告消息*/public static AjaxResult warn(String msg, Object data) {return new AjaxResult(HttpStatus.WARN, msg, data);}/*** 返回错误消息** @return 错误消息*/public static AjaxResult error() {return AjaxResult.error("操作失败");}/*** 返回错误消息** @param msg 返回内容* @return 错误消息*/public static AjaxResult error(String msg) {return AjaxResult.error(msg, null);}/*** 返回错误消息** @param msg  返回内容* @param data 数据对象* @return 错误消息*/public static AjaxResult error(String msg, Object data) {return new AjaxResult(HttpStatus.ERROR, msg, data);}/*** 返回错误消息** @param code 状态码* @param msg  返回内容* @return 错误消息*/public static AjaxResult error(int code, String msg) {return new AjaxResult(code, msg, null);}/*** 是否为成功消息** @return 结果*/public boolean isSuccess() {return Objects.equals(HttpStatus.SUCCESS, this.get(CODE_TAG));}/*** 是否为警告消息** @return 结果*/public boolean isWarn() {return Objects.equals(HttpStatus.WARN, this.get(CODE_TAG));}/*** 是否为错误消息** @return 结果*/public boolean isError() {return Objects.equals(HttpStatus.ERROR, this.get(CODE_TAG));}/*** 方便链式调用** @param key* @param value* @return*/@Overridepublic AjaxResult put(String key, Object value) {super.put(key, value);return this;}
}

ServiceException业务异常类封装

/*** 业务异常类封装*/
public final class ServiceException extends RuntimeException
{private static final long serialVersionUID = 1L;/*** 错误码*/private Integer code;/*** 错误提示*/private String message;/*** 错误明细,内部调试错误*/private String detailMessage;/*** 空构造方法,避免反序列化问题*/public ServiceException(){}public ServiceException(String message){this.message = message;}public ServiceException(String message, Integer code){this.message = message;this.code = code;}public String getDetailMessage(){return detailMessage;}@Overridepublic String getMessage(){return message;}public Integer getCode(){return code;}public ServiceException setMessage(String message){this.message = message;return this;}public ServiceException setDetailMessage(String detailMessage){this.detailMessage = detailMessage;return this;}
}

InnerAuthException 内部认证异常


/*** 内部认证异常*/
public class InnerAuthException extends RuntimeException {private static final long serialVersionUID = 1L;public InnerAuthException(String message) {super(message);}
}

ServiceException 业务异常


/*** 业务异常**/
public final class ServiceException extends RuntimeException {private static final long serialVersionUID = 1L;/*** 错误码*/private Integer code;/*** 错误提示*/private String message;/*** 错误明细,内部调试错误* <p>* 和 {@link CommonResult#getDetailMessage()} 一致的设计*/private String detailMessage;/*** 空构造方法,避免反序列化问题*/public ServiceException() {}public ServiceException(String message) {this.message = message;}public ServiceException(String message, Integer code) {this.message = message;this.code = code;}public String getDetailMessage() {return detailMessage;}@Overridepublic String getMessage() {return message;}public Integer getCode() {return code;}public ServiceException setMessage(String message) {this.message = message;return this;}public ServiceException setDetailMessage(String detailMessage) {this.detailMessage = detailMessage;return this;}
}

NotPermissionException 未能通过的权限认证异常


import org.apache.commons.lang3.StringUtils;/*** 未能通过的权限认证异常**/
public class NotPermissionException extends RuntimeException {private static final long serialVersionUID = 1L;public NotPermissionException(String permission) {super(permission);}public NotPermissionException(String[] permissions) {super(StringUtils.join(permissions, ","));}
}

NotRoleException 未能通过的角色认证异常


import org.apache.commons.lang3.StringUtils;/*** 未能通过的角色认证异常**/
public class NotRoleException extends RuntimeException {private static final long serialVersionUID = 1L;public NotRoleException(String role) {super(role);}public NotRoleException(String[] roles) {super(StringUtils.join(roles, ","));}
}

GlobalExceptionHandler核心全局拦截配置类

/*** 全局异常处理器*/
@RestControllerAdvice
public class GlobalExceptionHandler {private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);/*** 权限码异常*/@ExceptionHandler(NotPermissionException.class)public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {String requestURI = request.getRequestURI();log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");}/*** 角色权限异常*/@ExceptionHandler(NotRoleException.class)public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request) {String requestURI = request.getRequestURI();log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");}/*** 请求方式不支持*/@ExceptionHandler(HttpRequestMethodNotSupportedException.class)public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {String requestURI = request.getRequestURI();log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());return AjaxResult.error(e.getMessage());}/*** 业务异常*/@ExceptionHandler(ServiceException.class)public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) {log.error(e.getMessage(), e);Integer code = e.getCode();return code !=null ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());}/*** 请求路径中缺少必需的路径变量*/@ExceptionHandler(MissingPathVariableException.class)public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) {String requestURI = request.getRequestURI();log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI, e);return AjaxResult.error(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));}/*** 拦截未知的运行时异常*/@ExceptionHandler(RuntimeException.class)public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {String requestURI = request.getRequestURI();log.error("请求地址'{}',发生未知异常.", requestURI, e);return AjaxResult.error(e.getMessage());}/*** 系统异常*/@ExceptionHandler(Exception.class)public AjaxResult handleException(Exception e, HttpServletRequest request) {String requestURI = request.getRequestURI();log.error("请求地址'{}',发生系统异常.", requestURI, e);return AjaxResult.error(e.getMessage());}/*** 自定义验证异常*/@ExceptionHandler(BindException.class)public AjaxResult handleBindException(BindException e) {log.error(e.getMessage(), e);String message = e.getAllErrors().get(0).getDefaultMessage();return AjaxResult.error(message);}/*** 自定义验证异常*/@ExceptionHandler(MethodArgumentNotValidException.class)public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {log.error(e.getMessage(), e);String message = e.getBindingResult().getFieldError().getDefaultMessage();return AjaxResult.error(message);}/*** 内部认证异常*/@ExceptionHandler(InnerAuthException.class)public AjaxResult handleInnerAuthException(InnerAuthException e) {return AjaxResult.error(e.getMessage());}}
http://www.lryc.cn/news/578362.html

相关文章:

  • 【实时Linux实战系列】实时数据库与数据存储方案
  • 学习threejs,使用自定义GLSL 着色器,生成艺术作品
  • 使用Rust原生实现小波卡尔曼滤波算法
  • 408第三季part1 - 操作系统 - 基本分页
  • 算法赋能管理:工厂安全与效率双突破
  • 【仿muduo库实现并发服务器】Channel模块
  • 回转体航行器控制系统中深度控制与俯仰姿态控制的解耦策略
  • 基于springboot的养老院管理系统
  • C# Linq to XML 详解:强大的XML处理工具
  • (自用)Java学习-5.21(支付宝沙箱支付、Vue总结)
  • 插入排序解析
  • sqlmap学习笔记ing(1.Easy_SQLi(时间,表单注入))
  • Django打造智能Web机器人控制平台
  • HarmonyOS应用开发高级认证知识点梳理 (一) 布局与样式
  • 记本好书:矩阵力量:线性代数全彩图解+微课+Python编程
  • 深蓝海域承建某大型保险集团产险知识库升级项目
  • 主流零信任安全产品深度介绍
  • 11OAuth2
  • 从零到一搭建远程图像生成系统:Stable Diffusion 3.5+内网穿透技术深度实战
  • 【深度学习1】ModernBert学习
  • 发布/订阅模式:解耦系统的强大设计模式
  • Spring Boot 集成 Dufs 通过 WebDAV 实现文件管理
  • 从零到一:VNC+内网穿透技术搭建企业级远程控制系统的完整路径
  • ubuntu系统安装docker 和 mongdb,YaPi(包含中间过程不能拉去依赖问题)
  • langchain从入门到精通(三十二)——RAG优化策略(八)自查询检索器实现动态数据过滤
  • 自由学习记录(66)
  • 聚观早报 | 知乎直答新升级;特斯拉V4超级充电桩首批上线;苹果将推出廉价版Macbook
  • 缓存系统-淘汰策略
  • 边缘人工智能与医疗AI融合发展路径:技术融合与应用前景(下)
  • 定时器的设计