knife4j+springboot3.4异常无法正确展示文档

场景复现:

  • knife4j-openapi3-jakarta-spring-boot-starter版本

    com.github.xiaoymin knife4j-openapi3-jakarta-spring-boot-starter 4.5.0

  • 原来使用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.(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版本

    org.springdoc springdoc-openapi-starter-common 2.7.0

结论:期待knife4j-openapi3-jakarta-spring-boot-starter早日升级,兼容最新版本的spring;

开源SDK:https://github.com/mingyang66/spring-parent

相关推荐
途经六月的绽放4 小时前
常见设计模式及其应用示例
java·设计模式
REI-4 小时前
黑马点评项目启动
java·后端
AlunYegeer4 小时前
【JAVA】网关的管理原理和微服务的Interceptor区分
java·服务器·前端
xieliyu.4 小时前
Java、抽象类
java·开发语言
我真会写代码5 小时前
SpringBoot自动装配原理:告别繁琐配置,读懂底层逻辑
java·spring boot·mybatis
happymaker06265 小时前
servlet、jsp、请求转发、重定向的一些个人理解
java·开发语言·servlet
于先生吖5 小时前
国际版答题系统 JAVA 源码实战指南
java·开发语言
gelald5 小时前
JVM - 垃圾回收
java·jvm·后端
东离与糖宝5 小时前
模式匹配支持原生类型!JDK26 switch语法极简实战
java·人工智能
workflower5 小时前
如何使用设计模式-误区
java·开发语言·设计模式·集成测试·软件工程·需求分析·软件需求