Spring Boot启动报错:org.webjars与classgraph版本不兼容求助
Spring Boot启动报错:ClassGraph.acceptPaths方法不存在(版本兼容问题)
启动Spring Boot应用时触发报错,提示调用不存在的方法io.github.classgraph.ClassGraph.acceptPaths(java.lang.String[]),问题根源是org.webjars.WebJarAssetLocator与io.github.classgraph.ClassGraph版本不兼容。
控制台报错信息
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2024-03-14T13:24:39.051+05:00 ERROR 19984 --- [cms] [ main] o.s.b.d.LoggingFailureAnalysisReporter : APPLICATION FAILED TO START Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.webjars.WebJarAssetLocator.scanForWebJars(WebJarAssetLocator.java:188) The following method did not exist: 'io.github.classgraph.ClassGraph io.github.classgraph.ClassGraph.acceptPaths(java.lang.String[])' The calling method's class, org.webjars.WebJarAssetLocator, was loaded from the following location: jar:file:/C:/Users/Valentin/.m2/repository/org/webjars/webjars-locator-core/0.55/webjars-locator-core-0.55.jar!/org/webjars/WebJarAssetLocator.class The called method's class, io.github.classgraph.ClassGraph, is available from the following locations: jar:file:/C:/Users/Valentin/.m2/repository/io/github/classgraph/classgraph/4.8.69/classgraph-4.8.69.jar!/io/github/classgraph/ClassGraph.class The called method's class hierarchy was loaded from the following locations: io.github.classgraph.ClassGraph: file:/C:/Users/Valentin/.m2/repository/io/github/classgraph/classgraph/4.8.69/classgraph-4.8.69.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes org.webjars.WebJarAssetLocator and io.github.classgraph.ClassGraph Process finished with exit code 1
项目pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.3</version> </parent> <groupId>ru.aston.crm</groupId> <artifactId>cms</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cms</name> <description>Customer Management Service</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.5.9</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
解决方案
问题根源
springdoc-openapi-ui:1.5.9依赖了webjars-locator-core:0.55,而Spring Boot 3.2.3默认引入的classgraph版本是4.8.69。旧版webjars-locator-core调用的acceptPaths(String[])方法在新版classgraph中已被移除,导致版本兼容冲突。
解决方法
方案一:升级springdoc-openapi-ui到兼容Spring Boot 3.x的版本
Spring Boot 3.x需要使用springdoc-openapi v2.x系列版本,该版本已适配新版classgraph。修改pom中的依赖:<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>2.2.0</version> </dependency>方案二:强制指定兼容的webjars-locator-core版本
保留现有springdoc版本,直接指定与classgraph 4.8.69兼容的webjars-locator-core:0.52:<dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator-core</artifactId> <version>0.52</version> </dependency>方案三:排除旧版依赖后引入兼容版本
排除springdoc中的旧版webjars-locator-core,再手动引入兼容版本:<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.5.9</version> <exclusions> <exclusion> <groupId>org.webjars</groupId> <artifactId>webjars-locator-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator-core</artifactId> <version>0.52</version> </dependency>
内容的提问来源于stack exchange,提问作者noslav7
相关产品推荐
相关产品推荐

