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

java全局异常处理(springboot)

介绍:

在日常项目开发中,异常是常见的,但是如何更高效的处理好异常信息,让我们能快速定位到BUG,是很重要的,不仅能够提高我们的开发效率,还能让你代码看上去更舒服,SpringBoot的项目已经对有一定的异常处理了,但是对于我们开发者而言可能就不太合适了,因此我们需要对这些异常进行统一的捕获并处理。

我们只需在完整的springboot项目中添加四个java类即可

一:异常枚举类

/*** @version 1.0* @Author guozhen8* @Date 2023年11月23日 0023 17:57:01* @注释 异常返回枚举类*/
@Getter
@ToString
@AllArgsConstructor
public enum RespBeanEnum {SUCCESS(200,"SUCCESS"),ERROR(500,"服务端异常"),BIND_ERROR(500210,"参数校验异常"),NULL_ERROR(500211,"找不到数据"),INSERT_ERROR(500212,"数据插入异常-主键重复" );private final Integer code;private final String message;}

二:异常返回结果类

/*** @version 1.0* @Author guozhen8* @Date 2023年11月23日 0023 17:57:01* @注释 异常返回类*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RespBean {private long code;private String message;private Object obj;/*** 功能描述:返回成功结果* @param* @return*/public static RespBean success(){return new RespBean(RespBeanEnum.SUCCESS.getCode(),RespBeanEnum.SUCCESS.getMessage(),null);}/*** 功能描述:返回成功结果* @param obj* @return*/public static RespBean success(Object obj){return new RespBean(RespBeanEnum.SUCCESS.getCode(),RespBeanEnum.SUCCESS.getMessage(),obj);}/*** 功能描述:返回失败结果* @param respBeanEnum* @return*/public static RespBean error(RespBeanEnum respBeanEnum){return new RespBean(respBeanEnum.getCode(),respBeanEnum.getMessage(),null);}/*** 功能描述:返回失败结果* @param respBeanEnum,obj* @return*/public static RespBean error(RespBeanEnum respBeanEnum,Object obj){return new RespBean(respBeanEnum.getCode(),respBeanEnum.getMessage(),obj);}
}

三:全局异常

/*** @version 1.0* @Author guozhen8* @Date 2023年11月23日 0023 17:57:01* @注释 全局异常*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GlobalException extends RuntimeException{private RespBeanEnum respBeanEnum;
}

四:全局异常处理类

/*** @version 1.0* @Author guozhen8* @Date 2023年11月23日 0023 17:57:01* @注释 自定义异常处理类*/
@RestControllerAdvice
public class GlobalExceptionHandler {//调试日志private final Logger logger = LoggerFactory.getLogger(this.getClass());@ExceptionHandler(Exception.class)//处理哪些异常public RespBean ExceptionHandler(Exception e,HttpServletRequest request){//打印日志logger.error("Requst URL : {},Exception : {}", request.getRequestURL(),e);if(e instanceof GlobalException){//如果是之前自定义的异常GlobalException ex = (GlobalException) e;return RespBean.error(ex.getRespBeanEnum());}else if(e instanceof BindException) { //没有通过参数校验注解抛出的异常BindException ce = (BindException) e;RespBean respBean = RespBean.error(RespBeanEnum.BIND_ERROR);respBean.setMessage("参数校验异常:" + ce.getMessage());return respBean;}else if(e instanceof DuplicateKeyException) { //插入重复数据抛出的异常DuplicateKeyException de = (DuplicateKeyException) e;RespBean respBean = RespBean.error(RespBeanEnum.INSERT_ERROR);respBean.setMessage("参数插入异常:" + de.getMessage());return respBean;}else if(e instanceof  ConstraintViolationException) { //传入数据有误抛出的异常ConstraintViolationException de = ( ConstraintViolationException) e;RespBean respBean = RespBean.error(RespBeanEnum.BIND_ERROR);respBean.setMessage("参数校验异常:" + de.getMessage());return respBean;}return RespBean.error(RespBeanEnum.ERROR);}}

五:使用方法

在使用时,我们可以返回异常结果类或者直接抛出相关异常即可。

//1:返回异常结果
return  RespBean.error(RespBeanEnum.ERROR,"xxxx");
return RespBean.success("xxxx");
//2:抛出异常
throw new BindException("xxxx");

http://www.lryc.cn/news/246883.html

相关文章:

  • JAVA将PDF转图片
  • 合并区间[中等]
  • MYSQL基础知识之【LIKE子句的使用 ,NULL值的处理,空值的处理】
  • 线索二叉树:C++实现
  • C++——vector互换容器与预留空间
  • Unity 自带的一些可以操控时间的属性或方法。
  • vue 项目中使用 mqtt
  • linux shell操作 - 05 进程 与 IO 模型
  • 让SOME/IP运转起来——SOME/IP系统设计(下)之数据库开发
  • Mybatis反射工厂类DefaultReflectorFactory
  • antDesignPro a-table样式二次封装
  • 找免费4K高清图片素材,就上这6个网站
  • 代码随想录算法训练营第35天| 860.柠檬水找零 406.根据身高重建队列 452. 用最少数量的箭引爆气球
  • 成为AI产品经理——TPR、FPR、ROC、AUC
  • java: Internal error in the mapping processor: java.lang.NullPointerException
  • TCP知识点
  • 大语言模型(LLMs)在 Amazon SageMaker 上的动手实践(一)
  • 顶级数据恢复工具—— 最全的15个数据恢复软件榜单
  • 【图像分类】【深度学习】【Pytorch版本】Inception-ResNet模型算法详解
  • Ubuntu 22.03 LTS 安装deepin-terminal 实现 终端 分屏
  • HTTP协议,Web框架回顾
  • el-checkbox 对勾颜色调整
  • 系统管理精要:深度探索 Linux 监控与管理利器
  • vue3之echarts渐变柱状图
  • 有一种浪漫,叫接触Linux
  • 构建 App 的方法
  • laravel实现发送邮件功能
  • 概要设计检查单、需求规格说明检查单
  • 达梦列式存储和clickhouse基准测试
  • 【Web】NewStarCtf Week2 个人复现