文章目录
统一处理异常
@ControllerAdvice
-
用于修饰类,表示该类是Controller的全局配置类
-
在此类中,可以对Controller进行如下三种全局配置:异常处理方案、绑定数据方案、绑定参数方案
@ExceptionHandle
- 用于修饰方法,该方法会在 Controller 方法执行后被调用,用于为 处理捕获到的异常
@ModelAttribute
- 用于修饰方法,该方法会在 Controller 方法执行前被调用,用于为 Model 对象绑定参数
@DataBinder
- 用于修饰方法,该方法会在 Controller 方法执行前被调用,用于绑定参数的转换器
声明一个异常的通知,使组件只去扫描带有 controller 注解的类
package com.community.controller.advice;
//声明一个异常的通知
// 使组件只去扫描带有 controller 注解的类
@ControllerAdvice(annotations = Controller.class)
public class ExceptionAdvice {
private static final Logger logger = LoggerFactory.getLogger(Ex