@RestControllerAdvice 无法通过设置的annotations拦截指定Controller

问题和这个类似 java - Spring basePackageClasses from @ControllerAdvice doesn't work - Stack Overflow

我自定义了一个注解,

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface BaseResponse {
    
}

在Controller类中加上了这个注解。

异常处理器中,加上了这个注解

@RestControllerAdvice(annotations = {BaseResponse.class})

对应的@ExceptionHandler方法并没有进行捕获处理

但如果把异常处理器的注解改成

@RestControllerAdvice

就能捕获了。

如果我需要指定捕获范围,该怎么做呢?求大神解答

试试?

@RestControllerAdvice({"some.my.pkg"})
 or
@RestControllerAdvice(basePackages={"some.my.pkg"})

当然,这样的话你的@BaseResponse就用不到了