配置swagger/knife4j时出现的Unresolvable class definition for class …异常
- 问题
- 简要关键问题摘取
- 解决方法
问题
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/Users/congee/.m2/repository/io/springfox/springfox-spring-webmvc/3.0.0/springfox-spring-webmvc-3.0.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]; nested exception is java.lang.IllegalArgumentException: Unresolvable class definition for class [springfox.documentation.spring.web.OnServletBasedWebApplication]
2023-03-09 15:37:52.499 INFO 30762 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-03-09 15:37:52.509 ERROR 30762 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/Users/congee/.m2/repository/io/springfox/springfox-spring-webmvc/3.0.0/springfox-spring-webmvc-3.0.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]; nested exception is java.lang.IllegalArgumentException: Unresolvable class definition for class [springfox.documentation.spring.web.OnServletBasedWebApplication]
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:452) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:315) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:276) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:600) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:311) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:600) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:311) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:304) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:319) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.6.RELEASE.jar:2.3.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.6.RELEASE.jar:2.3.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.6.RELEASE.jar:2.3.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) [spring-boot-2.3.6.RELEASE.jar:2.3.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.6.RELEASE.jar:2.3.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.6.RELEASE.jar:2.3.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.6.RELEASE.jar:2.3.6.RELEASE]
at com.boc.address.AddServiceApplication.main(AddServiceApplication.java:24) [classes/:na]
Caused by: java.lang.IllegalArgumentException: Unresolvable class definition for class [springfox.documentation.spring.web.OnServletBasedWebApplication]
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:331) ~[spring-core-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConditionEvaluator.getCondition(ConditionEvaluator.java:124) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:96) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:88) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:71) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isConditionMatch(ClassPathScanningCandidateComponentProvider.java:513) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isCandidateComponent(ClassPathScanningCandidateComponentProvider.java:496) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:430) ~[spring-context-5.2.11.RELEASE.jar:5.2.11.RELEASE]
... 30 common frames omitted
Caused by: java.lang.NoClassDefFoundError: springfox/documentation/common/ClassPresentInClassPathCondition
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_312]
at java.lang.ClassLoader.defineClass(ClassLoader.java:757) ~[na:1.8.0_312]
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_312]
at java.net.URLClassLoader.defineClass(URLClassLoader.java:473) ~[na:1.8.0_312]
at java.net.URLClassLoader.access$100(URLClassLoader.java:74) ~[na:1.8.0_312]
at java.net.URLClassLoader$1.run(URLClassLoader.java:369) ~[na:1.8.0_312]
at java.net.URLClassLoader$1.run(URLClassLoader.java:363) ~[na:1.8.0_312]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_312]
at java.net.URLClassLoader.findClass(URLClassLoader.java:362) ~[na:1.8.0_312]
at java.lang.ClassLoader.loadClass(ClassLoader.java:419) ~[na:1.8.0_312]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) ~[na:1.8.0_312]
at java.lang.ClassLoader.loadClass(ClassLoader.java:352) ~[na:1.8.0_312]
at java.lang.Class.forName0(Native Method) ~[na:1.8.0_312]
at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_312]
at org.springframework.util.ClassUtils.forName(ClassUtils.java:284) ~[spring-core-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:324) ~[spring-core-5.2.11.RELEASE.jar:5.2.11.RELEASE]
... 37 common frames omitted
Caused by: java.lang.ClassNotFoundException: springfox.documentation.common.ClassPresentInClassPathCondition
at java.net.URLClassLoader.findClass(URLClassLoader.java:387) ~[na:1.8.0_312]
at java.lang.ClassLoader.loadClass(ClassLoader.java:419) ~[na:1.8.0_312]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) ~[na:1.8.0_312]
at java.lang.ClassLoader.loadClass(ClassLoader.java:352) ~[na:1.8.0_312]
... 53 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:61390', transport: 'socket'
Process finished with exit code 1
简要关键问题摘取
抽取出其中的关键原因描述:
- nested exception is java.lang.IllegalArgumentException: Unresolvable class definition for class [springfox.documentation.spring.web.OnServletBasedWebApplication]
- springfox.documentation.common.ClassPresentInClassPathCondition
解决方法
进行原因排查后,发现是依赖之间版本问题的冲突导致,下面提供一个能够正确运行的swaggerkinfe4j版本:
dependency>
groupId>io.springfoxgroupId>
artifactId>springfox-swagger2artifactId>
version>2.9.2version>
dependency>
dependency>
groupId>io.springfoxgroupId>
artifactId>springfox-swagger-uiartifactId>
version>2.9.2version>
dependency>
dependency>
groupId>io.swaggergroupId>
artifactId>swagger-annotationsartifactId>
version>1.5.22version>
dependency>
dependency>
groupId>io.swaggergroupId>
artifactId>swagger-modelsartifactId>
version>1.5.22version>
dependency>
dependency>
groupId>com.github.xiaoymingroupId>
artifactId>knife4j-spring-boot-starterartifactId>
version>2.0.3version>
dependency>
对应的springboot版本:
parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.3.6.RELEASEversion>
relativePath/>
parent>
后台成功运行后,打开api文档遇到的另一个问题:TypeError: n.forEach is not a function
解决方法见:https://blog.csdn.net/qq_45722699/article/details/129423121