区别@ControllerAdvice 和@RestControllerAdvice
2018-03-03 18:50
1511 查看
@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/
相关文章推荐
- @Controller @ResponseBody @ControllerAdvice @RestController @RestControllerAdvice 注解定义
- 使用 @ControllerAdvice 和 @RestControllerAdvice 通知控制器
- spirng 中禁用后缀的url访问,类似REST方式 ,spirng 4.x 之后的版本有@RestController支持
- Spring中@Controller和@RestController之间的区别
- spring mvc统一异常处理(@ControllerAdvice + @ExceptionHandler)
- spring mvc 使用 @ControllerAdvice 捕获404异常
- @Controller和@RestController的区别
- @Controller和@RestController的区别
- 3. Spring4.0之@RestController
- Spring Boot的被@SpringBootApplication注解的App.java必须放在所有的RestController的根路径的package下
- @Controller和@RestController的区别?
- Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json
- spring boot注解@SpringBootApplication、@Controller、@RestController
- spring boot注解@SpringBootApplication、@Controller、@RestController
- @Controller和@RestController的区别?
- SpringBoot 统一异常处理 @ControllerAdvice 失效
- Spring中@Controller和@RestController之间的区别
- spring boot之spring mvc常用配置--@controllerAdvice(5)
- spring mvc异常统一处理(ControllerAdvice注解)
- Spring MVC 4 RESTFul使用@RestController