gpt4 book ai didi

java - spring @Aspect 无法与 swagger2 一起使用

转载 作者:行者123 更新时间:2023-12-02 12:12:48 26 4
gpt4 key购买 nike

pom.xml版本信息:

  • springfox-swagger2:2.5.0
  • Swagger 核心:1.5.10
  • springfox-swagger-ui:2.6.1
  • springboot:1.5.3

我有一个使用 swagger2 和 springboot 的项目。

没有@Aspect的项目代码运行得很好。代码如下。

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();
}
}

正确的结果:

success picture

但是当我添加以下代码时,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");
}
}

错误结果:

fail picture

swagger和spring AOP有冲突吗?

最佳答案

@蛋

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

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

@EnableAspectJAutoProxy(proxyTargetClass=true)

仅当我们使用 Controller 接口(interface)时才会出现此问题。

引用 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.

关于java - spring @Aspect 无法与 swagger2 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46407927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com