knife4j+springboot3.4异常无法正确展示文档
作者:立小言先森
本文主要介绍了knife4j+springboot3.4异常无法正确展示文档,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
场景复现:
- knife4j-openapi3-jakarta-spring-boot-starter版本
<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-openapi3-jakarta-spring-boot-starter --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId> <version>4.5.0</version> </dependency>
原来使用springboot3.3.5版本,先升级到3.4.0版本
通过http://ip:port/doc.html访问接口文档发现访问/v3/api-docs接口时报
Handler dispatch failed: java.lang.NoSuchMethodError: 'void org.springframework.web.method.ControllerAdviceBean.<init>(java.lang.Object)
通过分析异常日志发现是ControllerAdviceBean类报错,在springboot3.3.5时spring-web版本是6.1.14,springboot3.4版本是6.2.0版本。
通过springboot全局异常捕获堆栈信息发现:
org.springdoc.core.service.GenericResponseService.lambda$getGenericMapResponse$8(GenericResponseService.java:702)
GenericResponseService类是在如下包:
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-common --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-common</artifactId> <version>2.3.0</version> </dependency>
GenericResponseService类的702行代码如下(ControllerAdviceBean类使用一个参数的构造函数,但是报异常):
List<ControllerAdviceInfo> controllerAdviceInfosNotInThisBean = controllerAdviceInfos.stream() .filter(controllerAdviceInfo -> new ControllerAdviceBean(controllerAdviceInfo.getControllerAdvice()).isApplicableToBeanType(beanType)) .filter(controllerAdviceInfo -> !beanType.equals(controllerAdviceInfo.getControllerAdvice().getClass())) .toList();
spring-web6.1.14版本中ControllerAdviceBean的构造函数:
public ControllerAdviceBean(Object bean) { Assert.notNull(bean, "Bean must not be null"); this.beanOrName = bean; this.isSingleton = true; this.resolvedBean = bean; this.beanType = ClassUtils.getUserClass(bean.getClass()); this.beanTypePredicate = createBeanTypePredicate(this.beanType); this.beanFactory = null; }
spring-web6.2.0版本中ControllerAdviceBean的构造函数:
public ControllerAdviceBean(String beanName, BeanFactory beanFactory, ControllerAdvice controllerAdvice) { Assert.hasText(beanName, "Bean name must contain text"); Assert.notNull(beanFactory, "BeanFactory must not be null"); Assert.isTrue(beanFactory.containsBean(beanName), () -> "BeanFactory [" + beanFactory + "] does not contain specified controller advice bean '" + beanName + "'"); Assert.notNull(controllerAdvice, "ControllerAdvice must not be null"); this.beanName = beanName; this.isSingleton = beanFactory.isSingleton(beanName); this.beanType = getBeanType(beanName, beanFactory); this.beanTypePredicate = createBeanTypePredicate(controllerAdvice); this.beanFactory = beanFactory; }
此类中的构造函数已经变更为4个,已经不存在一个参数的构造函数了。
- springdoc-openapi-starter-common文档当前已经升级到2.7.0版本
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-common --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-common</artifactId> <version>2.7.0</version> </dependency>
结论:期待knife4j-openapi3-jakarta-spring-boot-starter早日升级,兼容最新版本的spring;
开源SDK:https://github.com/mingyang66/spring-parent
到此这篇关于knife4j+springboot3.4异常无法正确展示文档的文章就介绍到这了,更多相关knife4j springboot3.4异常无法展示内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- Springboot3集成Knife4j的步骤以及使用(最完整版)
- SpringBoot Knife4j框架&Knife4j的显示内容的配置方式
- SpringBoot与knife4j的整合使用过程
- springboot读取bootstrap配置及knife4j版本兼容性问题及解决
- springboot3整合knife4j详细图文教程(swagger增强)
- springboot整合knife4j全过程
- knife4j 整合 springboot的过程详解
- SpringBoot中使用Knife4J的解决方案
- springboot集成swagger3与knife4j的详细代码
- Springboot中整合knife4j接口文档的过程详解