介绍
注解@ControllerAdvice用于修饰类 ,是非常有用的注解,是 SpringMVC 提供的功能,在 Spring Boot 中可以直接使用
该注解与其他注解配合使用 ,可以实现三个功能:
- 当 controlle抛错时,可进行全局异常处理
- 全局数据绑定。 https://www.cnblogs.com/lenve/p/10748453.html
- 全局数据预处理。 https://www.cnblogs.com/lenve/p/10748453.html
灵活使用这三个功能,可以帮助我们简化很多工作
注意
当 controlle抛错时,推荐使用该方式处理异常
当 过滤器抛错 ,该注解将无法处理
当 资源不存在时,该注解也能处理,比较麻烦,推荐使用 BasicExceptionController 处理
@ControllerAdvice 和 @RestControllerAdvice 区别
@ControllerAdvice:修饰类,类似 @Controller 可实现跳转页面
@RestControllerAdvice:修饰类,类似 @RestController 可实现返回json
参考:
https://www.cnblogs.com/lenve/p/10748453.html