• 统一异常处理


    统一异常处理

      让异常结果也显示为统一的返回结果对象,并且统一处理系统的异常信息,那么需要进行统一异常处理。

    1、创建统一异常处理器

    @Slf4j
    @Component //Spring容易自动管理
    @RestControllerAdvice //在controller层添加通知。如果使用@ControllerAdvice,则方法上需要添加@ResponseBody
    public class UnifiedExceptionHandler {
    
        /**
         * 未定义异常
         */
        @ExceptionHandler(value = Exception.class) //当controller中抛出Exception,则捕获
        public R handleException(Exception e) {
            log.error(e.getMessage(), e);
            return R.error();
        }
    }

    2、异常处理器要被Spring管理起来,需要设置被扫描进去

    @SpringBootApplication
    @ComponentScan({"xx.xx.xx","xx.xx.UnifiedExceptionHandler所在包"})
    public class 主启动类 {

    3、处理特定异常

      上诉返回的异常,就是实际的异常,一般不直接展示给界面用户,所以就需要个性化的显示异常信息,那么需要针对特定的异常做处理。

    3.1 添加相应的异常类jar,本次对应的是jdbc依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>

    3.2 添加相应的异常处理方法( UnifiedExceptionHandler 中)

    /**
    * 特定异常
    */
    @ExceptionHandler(BadSqlGrammarException.class)
    public R handleBadSqlGrammarException(BadSqlGrammarException e){
        log.error(e.getMessage(), e);
        return R.setResult(ResponseEnum.BAD_SQL_GRAMMAR_ERROR);
    }
    问题:上面的例子虽然针对特定的异常显示个性化的错误信息,但是针对每个不同的异常我们都需要在项目中添加对应的处理方法,并捕获对应的异常对象,可能还要针对这个异常添加额外的依赖。这显然不是最好的方式。
    优化方案:此类异常直接抛出,并且用Exception类捕获就可以了。

    4、自定义异常

    需求:使用一个或较少的异常类,可以捕获和显示所有的异常信息。
    方案:因此,我们可以创建一个自定义异常类(必须是运行时异常),在程序中抛出这个自定义异常对象,并在统一异常处理器中捕获自定义异常对象。
    4.1 创建自定义异常类
    @Data
    @NoArgsConstructor
    public class BusinessException extends RuntimeException {
    
        //状态码
        private Integer code;
    
        //错误消息
        private String message;
    
    }

    4.2 添加异常处理方法(UnifiedExceptionHandler类中)

    /**
    * 自定义异常
    */
    @ExceptionHandler(BusinessException.class)
    public R handleBusinessException(BusinessException e){
        log.error(e.getMessage(), e);
        return R.error().message(e.getMessage()).code(e.getCode());
    }

    4.3 修改Controller

    添加异常处理,业务中需要的位置抛出BusinessException自定义异常。

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save(
        @ApiParam(value = "积分等级对象", required = true)
        @RequestBody IntegralGrade integralGrade){
    
        //如果借款额度为空就手动抛出一个自定义的异常!
        if(integralGrade.getBorrowAmount() == null){
            //BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }
    
        boolean result = integrationService.save(integralGrade);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

     五、异常优化处理

    目标:以优雅的 Assert(断言) 方式来校验业务的异常情况,消除 if else

     5.1 模仿org.springframework.util.Assert类,自定义Assert类

    /**
     * 断言类:在程序运行的时候,帮助简化校验参数
     * 为了适配枚举类,模仿org.springframework.util.Assert类,创建自定义Assert类
     */
    @Slf4j
    public abstract class Assert {
    
        /**
         * 断言对象不为空
         * 如果对象obj为空,则抛出异常
         *
         * @param obj 待判断对象
         */
        public static void notNull(Object obj, ResponseEnum responseEnum) {
            if (obj == null) {
                log.info("obj is null...............");
                throw new BusinessException(responseEnum);
            }
        }
    
    }

    5.2 修改controller,对应的位置替换成Assert

            Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
    //        if(integralGrade.getBorrowAmount()==null){
    //            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
    //        }

     六、Controller上层异常

    6.1 异常分类

    6.2 处理Controller上层异常

    TODO:

  • 相关阅读:
    spring
    ajax
    jquary
    Java web 部分
    长跑马拉松
    面试的标准
    数据预处理——剔除异常值,平滑,归一化
    概率分布
    养生
    平滑的作用
  • 原文地址:https://www.cnblogs.com/gzhcsu/p/15947318.html
Copyright © 2020-2023  润新知