SpringBoot项目启动报错--ClassNotFoundE

2021-12-09  本文已影响0人  wyn_做自己

今日份鸡汤:做自己的太阳,与山川湖海为伴,热爱生活,自信勇敢,保持善良,勇往直前!

很久前弄的一个SpringBoot小demo,今天想要验证一个功能的时候,突然发现启动不了,日志信息如下:

2021-12-09 11:06:32.782  WARN 18820 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageConverters' defined in class path resource [org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.http.HttpMessageConverters]: Factory method 'messageConverters' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter.getPartConverters()Ljava/util/List;
2021-12-09 11:06:32.790  INFO 18820 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

除了上述这种,也有可能下面这种:

Caused by: java.lang.NoClassDefFoundError: org/springframework/web/util/pattern/PathPatternParser
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_131]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_131]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_131]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463) ~[spring-core-5.3.12.jar:5.3.12]
    ... 23 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.web.util.pattern.PathPatternParser
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_131]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_131]
    ... 27 common frames omitted

其实类似这种的大部分原因都是JAR包错误或JAR包冲突,那就去看看pom.xml 中的配置,看到了如下依赖:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.25.RELEASE</version>
        </dependency>

好了问题就在这里,SpringBoot 2.5.6 版本默认使用Spring的版本<spring-framework.version>5.3.12</spring-framework.version>,
至此就发现这个问题就是JAR包冲突导致的,只需要把pom中这个Spring依赖去掉,刷新一下maven依赖,重新启动项目就好了。

如果想查看SpringBoot中依赖的相关组件的默认版本,可以直接看这篇:SpringBoot项目查看(Spring、SpringBoot)默认版本配置

上一篇下一篇

猜你喜欢

热点阅读