区别@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中,大量注解的使用,使得代码 ...
随机推荐
- Python Web学习笔记之多线程编程
本次给大家介绍Python的多线程编程,标题如下: Python多线程简介 Python多线程之threading模块 Python多线程之Lock线程锁 Python多线程之Python的GIL锁 ...
- android CMakeLists
https://developer.android.google.cn/studio/projects/configure-cmake https://blog.csdn.net/songmingzh ...
- [省选模拟]array
这题真是太神了! 考试的时候冲着四十分写了个$O(\frac{N^3logN}{32})$的制杖算法. 然后就狠狠的T掉了.如果没有充分的理解单调性和应用单调性就只有10分的傻逼分拿了. 首先考虑枚举 ...
- 基于Android应用《玩转英语》(总报告)
基于Android应用<玩转英语> 摘 要 ...
- canvas压缩图片
1.canvas.toDataUrl压缩图片 canvas的toDataUrl方法可以将内容导出为base64编码格式的图片,采用base64编码将比源文件大1/3,但是该方法可以指定导出图片质量,所 ...
- JavaScript 装饰者模式(this运用)
例: function ConcreteClass() { this.performTask = function () { this.preTask(); console.log('doing so ...
- Ubuntu 14.04 禁用ipv6
参考: 关闭IPV6,ubuntu 14.04 Ubuntu 14.04 禁用ipv6 1.检查ipv6是否开启: cat /proc/sys/net/ipv6/conf/all/disable_ip ...
- spring boot 捕获filter异常 统一返回处理结果
如前面的文章所述,controller中抛出的异常我们使用ControllerAdvice来处理: @RestControllerAdvice @Slf4j public class GlobalEx ...
- Python matplot画柱状图(一)
图的存在,让数据变得形象化.无论多么复杂的东西,都是简单的组合. import matplotlib.pyplot as plt import numpy as np dict = {'A': 40, ...
- ubuntu14.04(server amd64)免密码sudo
vi /etc/sudoers.d/nopasswd4sudo 加入以下内容 用户名 ALL=(ALL) NOPASSWD : ALL