spring @Aspect 无法与 swagger2 一起使用

spring @Aspect can not work with swagger2

pom.xml 版本信息:

我有一个带有 swagger2 和 springboot 的项目。

没有@Aspect的项目代码非常好用well.The代码如下

public interface TestApi {
    WfExecution test(Long temp);
}


@Api(value = "TestAPI")
@RequestMapping(value = "/test")
@RestController
public class TestApiImpl implements TestApi {

    @Override
    @RequestMapping(value = "/test")
    @ApiOperation(value = "", notes = "", produces = MediaType.APPLICATION_JSON)
    public WfExecution test(@ApiParam(value = "", required = true) @RequestParam(required = true, value = "temp")
                                        Long temp) {
        return new WfExecution();
    }
}

正确结果:

但是当我添加以下代码时,swagger-ui 不显示 test-api-impl。

@Aspect
@Component
public class LoggerAop {
    @Before("execution(* com.XXX.controller.impl.TestApiImpl.*(..))")
    public void doBeforeAdvice(JoinPoint joinPoint){
            System.out.println("XXX");
    }
}

错误结果:

swagger和springAOP有冲突吗?

@egg

我设置了类似的项目并遇到了同样的问题。

在 @EnableAspectJAutoProxy 注释中将 proxyTargetClass 属性 设置为 true 后,问题得到解决。

@EnableAspectJAutoProxy(proxyTargetClass=true)

只有当我们使用控制器接口时才会出现此问题。

从 Java 文档中引用此 属性 EnableAspectJAutoProxy 的用法。

Users can control the type of proxy that gets created for {@code FooService} using the {@link #proxyTargetClass()} attribute. The following enables CGLIB-style 'subclass' proxies as opposed to the default interface-based JDK proxy approach.

这个答案是为那些仍然面临这个问题的人准备的 @Aspect 基本上是一个过滤器。如果您排除 swagges 资源,它将开始工作。

你可以使用

  @Pointcut("within(com..*..*Controller)")
  @Pointcut("within(com..*..*Service)")

所以它将只扫描那些从 com...

开始的包