全局异常处理

全局异常处理

@ControllerAdvice 注解

ControllerAdvice本质上是一个Component

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice

Specialization of @Component for classes that declare @ExceptionHandler, @InitBinder, or @ModelAttribute methods to be shared across multiple @Controller classes.

设定拦截规则, 根据拦截规则进行拦截,通过@ExceptionHandler、@InitBinder 或 @ModelAttribute这三个注解以及被其注解的方法来实现具体逻辑。

默认什么都不写,拦截所有 Controller

详细参数

@AliasFor("basePackages")
String[] value() default {};

@AliasFor("value")
String[] basePackages() default {};

Class<?>[] basePackageClasses() default {};

Class<?>[] assignableTypes() default {};

Class<? extends Annotation>[] annotations() default {};

@ControllerAdvice("org.my.pkg")
@ControllerAdvice(basePackages="org.my.pkg")
@ControllerAdvice(basePackages={"org.my.pkg", "org.my.other.pkg"})
拦截对应包及其子包下所有的Controller

也可以通过指定注解来匹配,比如我自定了一个 @CustomAnnotation 注解,我想匹配所有被这个注解修饰的 Controller, 可以这么写:@ControllerAdvice(annotations={CustomAnnotation.class})

/**
 * 全局异常处理
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 异常处理方法
     *
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex) {
        log.error(ex.getMessage());

        if (ex.getMessage().contains("Duplicate entry")) {
            String[] split = ex.getMessage().split(" ");
            String msg = split[2] + "已存在";
            return R.error(msg);
        }
        return R.error("未知错误");
    }
}
posted @ 2022-11-11 18:10  dayNo  阅读(44)  评论(0)    收藏  举报