@ControllerAdvice 和 @ExceptionHandler

  @ControllerAdvice 

 

 

The @ControllerAdvice annotation is a component annotation allowing implementation classes to be auto-detected through classpath scanning. It is automatically enabled when using the MVC namespace or the MVC Java config.

 

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

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

// Target all Controllers assignable to specific classes
@ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class})
public class AssignableTypesAdvice {}

 

 

 

 

 

@ExceptionHandler

扫描二维码关注公众号,回复: 861490 查看本文章

You can declare an @ExceptionHandler method within an @ControllerAdvice class in which case it handles exceptions from @RequestMappingmethods from many controllers.

 

 

 

 

猜你喜欢

转载自yangzhonglei.iteye.com/blog/2422823