控制器建议

@ExceptionHandler@InitBinder@ModelAttribute 方法仅适用于它们所声明的 @Controller 类或类层次结构。如果将它们声明在 @ControllerAdvice@RestControllerAdvice 类中,则它们适用于任何控制器。此外,从 5.3 版本开始,@ControllerAdvice 中的 @ExceptionHandler 方法可用于处理来自任何 @Controller 或任何其他处理程序的异常。

@ControllerAdvice@Component 元注解标记,因此可以通过组件扫描注册为 Spring Bean。

@RestControllerAdvice 是一个快捷注解,它将 @ControllerAdvice@ResponseBody 结合在一起,实际上它只是一个 @ControllerAdvice,其异常处理方法会渲染到响应体中。

在启动时,RequestMappingHandlerMappingExceptionHandlerExceptionResolver 会检测控制器通知 Bean 并在运行时应用它们。来自 @ControllerAdvice 的全局 @ExceptionHandler 方法在来自 @Controller 的局部方法之后应用。相反,全局 @ModelAttribute@InitBinder 方法在局部方法之前应用。

默认情况下,@ControllerAdvice@RestControllerAdvice 都适用于任何控制器,包括 @Controller@RestController。使用注解的属性来缩小它们适用的控制器和处理程序集合。例如:

  • Java

  • Kotlin

// Target all Controllers annotated with @RestController
@ControllerAdvice(annotations = RestController.class)
public class ExampleAdvice1 {}

// Target all Controllers within specific packages
@ControllerAdvice("org.example.controllers")
public class ExampleAdvice2 {}

// Target all Controllers assignable to specific classes
@ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class})
public class ExampleAdvice3 {}
// Target all Controllers annotated with @RestController
@ControllerAdvice(annotations = [RestController::class])
class ExampleAdvice1

// Target all Controllers within specific packages
@ControllerAdvice("org.example.controllers")
class ExampleAdvice2

// Target all Controllers assignable to specific classes
@ControllerAdvice(assignableTypes = [ControllerInterface::class, AbstractController::class])
class ExampleAdvice3

上述示例中的选择器在运行时进行评估,如果大量使用,可能会对性能产生负面影响。有关更多详细信息,请参阅 @ControllerAdvice Javadoc。

© . This site is unofficial and not affiliated with VMware.