java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > knife4j springboot3.4异常无法展示

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

作者:立小言先森

本文主要介绍了knife4j+springboot3.4异常无法正确展示文档,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

场景复现:

<!-- 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>
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个,已经不存在一个参数的构造函数了。

<!-- 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异常无法展示内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文