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

相关推荐
AI人工智能+电脑小能手7 小时前
【大白话说Java面试题 第87题】【Mysql篇】第17题:分布式事务的实现原理?
java·数据库·分布式·mysql·面试
来杯@Java8 小时前
图书管理系统(基于springboot+vue前后端分离的项目)计算机毕业设计java
java·spring boot·spring·vue·毕业设计·mybatis·课程设计
卷毛的技术笔记9 小时前
告别硬编码!Spring AI Alibaba 实现 AI Agent 智能工具调用(Tool Calling)
java·人工智能·后端·python·spring·ai编程
编程大师哥9 小时前
匿名函数 lambda + 高阶函数
java·python·算法
東雪木9 小时前
多线程与并发编程 专属复习笔记
java·开发语言·笔记·java面试
adrninistrat0r9 小时前
Java调用链MCP分析工具
java·python·ai编程
噜噜噜阿鲁~10 小时前
python学习笔记 | 11.3、面向对象高级编程-多重继承
java·开发语言
春生野草10 小时前
反射、Tomcat执行
java·开发语言
_日拱一卒11 小时前
LeetCode:207课程表
java·数据结构·算法·leetcode·职场和发展
飞翔中文网11 小时前
Java学习笔记之抽象类与接口(设计思想)
java·笔记·学习