zoukankan      html  css  js  c++  java
  • 区别@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/

  • 相关阅读:
    css 渐变动画
    div 画园
    sql server中使用xp_cmdshell
    (0.2.3)Mysql安装——二进制安装
    (0.2.2)如何下载mysql数据库(二进制、RPM、源码、YUM源)
    (0.2.1)mysql数据库环境-操作系统配置
    (0.2)linux下Mysql的安装配置与管理入门(目录篇)
    行列转换之——多行转多列,多列转多行实践版
    sql server数据库状态监控
    【bat】bat批处理异备文件、压缩文件(net use)
  • 原文地址:https://www.cnblogs.com/liaojie970/p/8502653.html
Copyright © 2011-2022 走看看