全局异常处理

简介: 全局异常处理@ControllerAdvice注解:不写参数:所有Controller参数为包名:包下的所有Controller,可指定多个如:@ControllerAdvice(basePackages={"cn.ken.test1", "cn.ken.test2"})

全局异常处理

@ControllerAdvice注解:

  • 不写参数:所有Controller参数为包名:包下的所有Controller,可指定多个如:@ControllerAdvice(basePackages={"cn.ken.test1", "cn.ken.test2"})参数为注解:可以自定义注解后匹配所有加了该注解的Controller,如:@ControllerAdvice(annotations={MyAnnotation.class})

此注解是一个在类上声明的注解,是aop思想的一种实现,主要通过配合@ExceptionHandler、@InitBinder 或 @ModelAttribute这三个注解以实现全局异常处理、全局数据绑定和全局数据预处理

@ExceptionHandler注解指定要捕获的异常类型

@ControllerAdvice
public class AllControllerAdvice {
    private static Logger logger = LoggerFactory.getLogger(AllControllerAdvice.class);
    /**
     * 应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
    }
    /**
     * 把值绑定到Model中,使全局@RequestMapping可以获取到该值
     */
    @ModelAttribute
    public void addAttributes(Model model) {
    }
    /**
     * 捕捉shiro的异常
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(ShiroException.class)
    @ResponseBody
    public ResponseModel<String> handleShiroException(ShiroException e) {
        return ResponseHelper.failedWith(null, CodeEnum.IDENTIFICATION_ERROR.getCode(),CodeEnum.IDENTIFICATION_ERROR.getMsg());
    }
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public ResponseModel<String> handleShiroException(AuthenticationException e) {
        return ResponseHelper.failedWith(null, CodeEnum.IDENTIFICATION_ERROR.getCode(),CodeEnum.IDENTIFICATION_ERROR.getMsg());
    }
    /**
     * 捕捉BusinessException自定义抛出的异常
     * @return
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ResponseModel handleBusinessException(BusinessException e) {
        String message = e.getMessage();
        if(message.indexOf(":--:") > 0){
            String[] split = message.split(":--:");
            return ResponseHelper.failedWith(null,split[1],split[0]);
        }
        return ResponseHelper.failedWith(null,CodeEnum.DATA_ERROR.getCode(),message);
    }
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(TemplateInputException.class)
    @ResponseBody
    public ResponseModel<String> handleTemplateInputException(TemplateInputException e) {
        return ResponseHelper.failed2Message(CodeEnum.USER_NO_PERMITION.getMsg());
    }
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = ParamJsonException.class)
    @ResponseBody
    public ResponseModel<String> handleParamJsonException(Exception e) {
        if(e instanceof ParamJsonException) {
            logger.info("参数错误:"+e.getMessage());
            return ResponseHelper.failed2Message("参数错误:"+ e.getMessage());
        }
        return ResponseHelper.failedWith(null,CodeEnum.PARAM_ERROR.getCode(),e.getMessage());
    }
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    @ResponseBody
    public ResponseModel<String> handleParamJsonException(HttpMessageNotReadableException e) {
        if(e instanceof HttpMessageNotReadableException) {
            logger.info("参数错误:"+e.getMessage());
            return ResponseHelper.failed2Message("参数错误:"+ e.getMessage());
        }
        return ResponseHelper.failedWith(null,CodeEnum.PARAM_ERROR.getCode(),e.getMessage());
    }
    /**
     * 全局异常捕捉处理
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public ResponseModel<String> errorHandler(Exception ex) {
        ex.printStackTrace();
        logger.error("接口出现严重异常:{}", ex.getMessage());
        return ResponseHelper.failed2Message(CodeEnum.ERROR.getMsg());
    }
}

可在类下定义多个@ExceptionHandler以对不同异常进行不同的处理


ExceptionHandler的处理顺序是由异常匹配度来决定的,首先找到可以匹配异常的所有ExceptionHandler,然后对其进行排序(深度比较器,通过递归不停地判断父异常是否为目标异常来取得最终的深度),取深度最小,即匹配度最高的那个

相关文章
|
2月前
全局异常处理
全局异常处理
33 1
|
2月前
|
程序员
项目中的全局异常是如何处理的
项目中的全局异常处理通常包括对预期异常(程序员手动抛出)和运行时异常的管理。项目已提供`BaseException`作为基础异常类,用于手动抛出异常,并通过`GlobalExceptionHandler`进行全局处理。`
39 4
|
2月前
|
Java
SpringBoot全局异常@RestControllerAdvice全局异常
SpringBoot全局异常@RestControllerAdvice全局异常
17 0
|
12月前
|
运维 Prometheus 监控
java异常 | 处理规范、全局异常、Error处理
java异常 | 处理规范、全局异常、Error处理
|
2月前
|
前端开发 IDE Java
使用aop实现全局异常处理
使用aop实现全局异常处理
76 0
|
7月前
|
JSON 前端开发 Java
SpringMVC中异常处理与ControllerAdvice捕捉全局异常
SpringMVC中异常处理与ControllerAdvice捕捉全局异常
71 0
|
10月前
全局统一异常处理
全局统一异常处理
42 0
|
11月前
全局参数、异常处理
全局参数、异常处理
117 0
|
12月前
|
Java
定义全局异常和全局异常处理器
定义全局异常和全局异常处理器
|
11月前
全局异常处理,为代码保驾护航
全局异常处理,为代码保驾护航
29 0