区别@ControllerAdvice 和@RestControllerAdvice
@ControllerAdvice和@RestControllerAdvice都可以指向控制器的一个子集:
// 指向所有带有注解@RestController的控制器
@ControllerAdvice(annotations = RestController.class)
public class AnnotationAdvice {}
// 指向所有指定包中的控制器
@ControllerAdvice("org.example.controllers")
public class BasePackageAdvice {}
// 指向所有带有指定签名的控制器
@ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class})
public class AssignableTypesAdvice {}
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController
A convenience annotation that is itself annotated with @Controller and @ResponseBody.
Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default. NOTE: @RestController is processed if an appropriate HandlerMapping-HandlerAdapter pair is configured such as the RequestMappingHandlerMapping-RequestMappingHandlerAdapter
pair which are the default in the MVC Java config and the MVC namespace.
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Component
public @interface ControllerAdvice
Indicates the annotated class assists a "Controller".
Serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning. It is typically used to define @ExceptionHandler, @InitBinder, and @ModelAttribute methods that apply to all @RequestMapping methods. One of annotations(), basePackageClasses(), basePackages() or its alias value() may be specified to define specific subsets of Controllers to assist.
When multiple selectors are applied, OR logic is applied - meaning selected Controllers should match at least one selector. The default behavior (i.e. if used without any selector), the @ControllerAdvice annotated class will assist all known Controllers. Note that those checks are done at runtime, so adding many attributes and using multiple strategies may have negative impacts (complexity, performance).
https://docs.spring.io/spring-framework/docs/5.0.0.M1/javadoc-api/
区别@ControllerAdvice 和@RestControllerAdvice的更多相关文章
- SpringMVC常用注解(三)
一.@Controller .@RestController 和 @ControllerAdvice 1. @Controller @Controller 用于标记在一个类上,使用它标记的类就是一个S ...
- springboot 详解RestControllerAdvice(ControllerAdvice)(转)
springboot 详解RestControllerAdvice(ControllerAdvice)拦截异常并统一处理简介 @Target({ElementType.TYPE}) @Retentio ...
- Spring Boot 系列 @ControllerAdvice 拦截异常并统一处理
ControllerAdvice用法解析 简介 通过@ControllerAdvice注解可以将对于控制器的全局配置放在同一个位置. 注解了@Controller的类的方法可以使用@Exception ...
- @RestControllerAdvice注解使用
在spring 3.2中,新增了@ControllerAdvice,@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...
- 精通Spring Boot---使用@ControllerAdvice处理异常
在Spring 3.2中,新增了@ControllerAdvice.@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...
- 全局异常捕获处理-@ControllerAdvice+@HandleException
涂涂影院管理系统这个demo中有个异常管理的标签,用于捕获 涂涂影院APP用户异常信息 ,有小伙伴好奇,排除APP,后台端的是如何处理全局异常的,故项目中的实际应用已记之. 关于目前的异常处理 在使用 ...
- SpringBoot - @ControllerAdvice 处理异常
在Spring 3.2中,新增了@ControllerAdvice.@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...
- 使用@ControllerAdvice处理异常
在Spring 3.2中,新增了@ControllerAdvice.@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...
- SpringBoot之常用注解
在spring boot中,摒弃了spring以往项目中大量繁琐的配置,遵循约定大于配置的原则,通过自身默认配置,极大的降低了项目搭建的复杂度.同样在spring boot中,大量注解的使用,使得代码 ...
随机推荐
- 优化 MySQL: 3 个简单的小调整
我并不期望成为一个专家级的 DBA,但是,在我优化 MySQL 时,我推崇 80/20 原则,明确说就是通过简单的调整一些配置,你可以压榨出高达 80% 的性能提升.尤其是在服务器资源越来越便宜的当下 ...
- 【前端开发】利用Fiddler抓包工具进行本地调试
解决什么问题: 解决前端在本地联调页面 || 样式 || 脚本时经常修改服务器代码,浪费太多时间. 避免多人同时修改代码产生冲突问题.可以在本地调完代码之后,再贴到服务器上. 其实这个问题老早就开始想 ...
- 20145127《java程序设计》第四周学习总结
教材学习内容总结 第六章 继承与多态 6.1 何为继承 0.面向对象中,子类继承父类,避免城府的行为定义.正确判断使用继承的时机,以及继承之后如何活用多态,才是学习继承时的重点. 1.继承:避免多个类 ...
- 微信小程序——3、逻辑js文件
逻辑层js文件 微信小程序前端进行了层次划分,分为逻辑层和视图层.逻辑层实现对数据的加工和处理.与HTML页面相似,逻辑层使用JavaScript编写.逻辑层将数据处理后发送至视图层,同时接受视图层的 ...
- mac OSX 实用快捷键
Command + shift + G. 前往文件夹 按键 效果 Shift + option + 音量+/- 以四分之一的刻度加 / 减音量 Shift. + option + 9 ······
- C#工程详解
转:https://www.cnblogs.com/zhaoqingqing/p/5468072.html 前言 写这篇文章的目地是为了让更多的小伙伴对VS生成的工程有一个清晰的认识.在开发过程中,为 ...
- Python3基础 assert 断言 确保程序的正确运行条件
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- JavaScript:Delete属性
以前,我就晓得delete只能够删除隐性属性(就是没有进行声明的变量),但是不知道为什么这样? 隐性属性:在页面中以前没有声明过该变量,直接进行赋值的 str='hongda' 其实这是由属性的特性决 ...
- swift设计模式学习 - 原型模式
移动端访问不佳,请访问我的个人博客 设计模式学习的demo地址,欢迎大家学习交流 原型模式 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 定义 用原型实例指定创建对象的种类,并且通 ...
- 【Git安装】centos安装git
1 yum install git 安装后的默认存放地点/usr/bin/git