MangoCool

一次缺少fastjson依赖引发的NoClassDefFoundError异常

2022-09-30 09:50:34   作者:anobody   来源:MangoCool

环境:centos7,jdk1.8,tomcat9,springboot2.4.10

问题:启动tomcat,应用报错:

ERROR org.springframework.boot.SpringApplication 860 reportFailure - Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageConverters' defined in class path resource [com/aolian/platform/cipher/machine/configurer/CipherWebConfig.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.NoClassDefFoundError: org/springframework/http/converter/GenericHttpMessageConverter
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.9.jar:5.3.9]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar:5.3.9]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.10.jar:2.4.10]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:771) ~[spring-boot-2.4.10.jar:2.4.10]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:763) ~[spring-boot-2.4.10.jar:2.4.10]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:438) ~[spring-boot-2.4.10.jar:2.4.10]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) ~[spring-boot-2.4.10.jar:2.4.10]
	at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:173) ~[spring-boot-2.4.10.jar:2.4.10]
	at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:153) ~[spring-boot-2.4.10.jar:2.4.10]
	at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:95) ~[spring-boot-2.4.10.jar:2.4.10]
	at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:174) ~[spring-web-5.3.9.jar:5.3.9]
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5219) ~[catalina.jar:9.0.64]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[catalina.jar:9.0.64]
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726) ~[catalina.jar:9.0.64]
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698) ~[catalina.jar:9.0.64]
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:696) ~[catalina.jar:9.0.64]
	at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1024) ~[catalina.jar:9.0.64]
	at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1911) ~[catalina.jar:9.0.64]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_332]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_332]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) ~[?:1.8.0_332]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) ~[?:1.8.0_332]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[?:1.8.0_332]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[?:1.8.0_332]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-util.jar:9.0.64]
	at java.lang.Thread.run(Thread.java:750) ~[?:1.8.0_332]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.http.HttpMessageConverters]: Factory method 'messageConverters' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/http/converter/GenericHttpMessageConverter
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.9.jar:5.3.9]
	... 36 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/http/converter/GenericHttpMessageConverter
	at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_332]
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756) ~[?:1.8.0_332]
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[?:1.8.0_332]
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:473) ~[?:1.8.0_332]
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74) ~[?:1.8.0_332]
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369) ~[?:1.8.0_332]
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363) ~[?:1.8.0_332]
	at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_332]
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362) ~[?:1.8.0_332]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[?:1.8.0_332]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[?:1.8.0_332]
	at java.lang.Class.forName0(Native Method) ~[?:1.8.0_332]
	at java.lang.Class.forName(Class.java:348) ~[?:1.8.0_332]
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1396) ~[catalina.jar:9.0.64]
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220) ~[catalina.jar:9.0.64]
	at com.xx.platform.cipher.machine.configurer.CipherWebConfig.messageConverters(CipherWebConfig.java:124) ~[classes/:0.0.1-SNAPSHOT]
	at com.xx.platform.cipher.machine.configurer.CipherWebConfig$$EnhancerBySpringCGLIB$$efb23fda.CGLIB$messageConverters$1(<generated>) ~[classes/:0.0.1-SNAPSHOT]
	at com.xx.platform.cipher.machine.configurer.CipherWebConfig$$EnhancerBySpringCGLIB$$efb23fda$$FastClassBySpringCGLIB$$c9228f79.invoke(<generated>) ~[classes/:0.0.1-SNAPSHOT]
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.9.jar:5.3.9]
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.9.jar:5.3.9]
	at com.xx.platform.cipher.machine.configurer.CipherWebConfig$$EnhancerBySpringCGLIB$$efb23fda.messageConverters(<generated>) ~[classes/:0.0.1-SNAPSHOT]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_332]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_332]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_332]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_332]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.9.jar:5.3.9]
	... 36 more
Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.GenericHttpMessageConverter
	at java.net.URLClassLoader.findClass(URLClassLoader.java:387) ~[?:1.8.0_332]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[?:1.8.0_332]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[?:1.8.0_332]
	at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_332]
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756) ~[?:1.8.0_332]
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[?:1.8.0_332]
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:473) ~[?:1.8.0_332]
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74) ~[?:1.8.0_332]
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369) ~[?:1.8.0_332]
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363) ~[?:1.8.0_332]
	at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_332]
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362) ~[?:1.8.0_332]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[?:1.8.0_332]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[?:1.8.0_332]
	at java.lang.Class.forName0(Native Method) ~[?:1.8.0_332]
	at java.lang.Class.forName(Class.java:348) ~[?:1.8.0_332]
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1396) ~[catalina.jar:9.0.64]
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220) ~[catalina.jar:9.0.64]
	at com.xx.platform.cipher.machine.configurer.CipherWebConfig.messageConverters(CipherWebConfig.java:124) ~[classes/:0.0.1-SNAPSHOT]
	at com.xx.platform.cipher.machine.configurer.CipherWebConfig$$EnhancerBySpringCGLIB$$efb23fda.CGLIB$messageConverters$1(<generated>) ~[classes/:0.0.1-SNAPSHOT]
	at com.xx.platform.cipher.machine.configurer.CipherWebConfig$$EnhancerBySpringCGLIB$$efb23fda$$FastClassBySpringCGLIB$$c9228f79.invoke(<generated>) ~[classes/:0.0.1-SNAPSHOT]
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.9.jar:5.3.9]
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.9.jar:5.3.9]
	at com.xx.platform.cipher.machine.configurer.CipherWebConfig$$EnhancerBySpringCGLIB$$efb23fda.messageConverters(<generated>) ~[classes/:0.0.1-SNAPSHOT]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_332]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_332]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_332]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_332]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.9.jar:5.3.9]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.9.jar:5.3.9]
	... 36 more

从异常提示看,是缺少spring-web依赖,其实不然,我反复检查了工程中是有spring-web包的。

于是只能从源头查,查看方法com.xx.platform.cipher.machine.configurer.CipherWebConfig.messageConverters:

@Bean
public HttpMessageConverters messageConverters() {
    return new HttpMessageConverters(new FastJsonHttpMessageConverter());
}

这里方法很简单,就用到了spring-boot-autoconfigure和alibaba的fastjson,继续检查依赖,发现工程下没有fastjson,但是在tomcat的lib下有呀,不明白?

没辙了,于是猜想可能跟springboot的初始化有关系,运行时的依赖可以从工程的lib->tomcat的lib下依次寻找,但是springboot初始化需要的依赖只能从工程的lib寻找

既然假设了,就行动起来,添加fastjson之后居然问题解决了!

猜想可能不对,这里记录一下问题,持续关注!

标签: java tomcat 依赖 NoClassDefFoundError

分享:

下一篇tortoiseGit error: unable to create file xxx.java: Filename too long

关于我

崇尚极简,热爱技术,喜欢唱歌,热衷旅行,爱好电子产品的一介码农。

座右铭

当你的才华还撑不起你的野心的时候,你就应该静下心来学习,永不止步!

人生之旅历途甚长,所争决不在一年半月,万不可因此着急失望,招精神之萎葸。

Copyright 2015- 芒果酷(mangocool.com) All rights reserved. 湘ICP备14019394号

免责声明:本网站部分文章转载其他媒体,意在为公众提供免费服务。如有信息侵犯了您的权益,可与本网站联系,本网站将尽快予以撤除。