java.lang.ClassNotFoundException: org.springframew

2019-02-27 01:33发布

问题:

I am using spring-integration in my project. When I deploy my app in the jetty container, I am getting the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'integrationRequestMappingHandlerMapping': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inboundhttpJobRequestGateway': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/http/converter/json/MappingJackson2HttpMessageConverter
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at com.dsths.spring.web.DsthsContextLoaderListener.configureAndRefreshWebApplicationContext(DsthsContextLoaderListener.java:97)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:771)
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:411)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:763)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:247)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1238)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:706)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:480)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at com.dsths.common.container.core.deploy.DeploymentManager.deploy(DeploymentManager.java:54)
    at com.dsths.common.container.core.deploy.DeploymentManager$$FastClassByCGLIB$$f5bc43eb.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:95)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inboundhttpJobRequestGateway': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/http/converter/json/MappingJackson2HttpMessageConverter
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
    at org.springframework.integration.http.inbound.IntegrationRequestMappingHandlerMapping.detectHandlerMethods(IntegrationRequestMappingHandlerMapping.java:103)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:109)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initApplicationContext(AbstractHandlerMethodMapping.java:89)
    at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119)
    at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:72)
    at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73)
    at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:117)
    at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:394)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1448)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 29 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/http/converter/json/MappingJackson2HttpMessageConverter
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:110)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
    ... 49 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/http/converter/json/MappingJackson2HttpMessageConverter
    at org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.<init>(HttpRequestHandlingEndpointSupport.java:166)
    at org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.<init>(HttpRequestHandlingMessagingGateway.java:72)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 51 more
Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.dsths.common.container.ContainerClassLoader.loadClass(ContainerClassLoader.java:136)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:429)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:382)
    ... 58 more

I have both the jars jackson-core-asl-1.8.3 and jackson-core-2.3.1 in the class path. Still I am getting the error. How to resolve the problem?

回答1:

It's interesting what is com.dsths.common.container.ContainerClassLoader...

Looks like spring-integration-http is loaded by this ClassLoader, but MappingJackson2HttpMessageConverter and all others spring-web classes with another ClassLoader.

Would be great, if you place all spring jars within the same class loader.

The app is not able to find MappingJackson2HttpMessageConverter in the class path. The problem is one class loader is picking one version and another class loader is picking another version.



回答2:

I had this weird error, too. I did a search in my jars in the web server's (tomcat) webapps/myapp/WEB-INF/lib and the class was there -- and only once.

It went away when I looked at another project at my job and used the same jackson jars.

Originally, I had the following:

jackson-core-2.4.3.jar
jackson-databind-2.4.3.jar

I removed these and added the following (packaged into my WEB-INF/lib):

jackson-mapper-asl-1.9.13.jar
jackson-datatype-jsr310-2.5.0.jar
jackson-datatype-joda-2.5.0.jar
jackson-databind-2.5.0.jar
jackson-core-2.5.0.jar
jackson-annotations-2.5.0.jar

I'm running this on Tomcat 7 on windows; using Java 1.6 (for compiling and runtime in Tomcat)

servlet.xml:

<beans:bean id="jacksonMessageConverter"  
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
</beans:bean>

    <beans:bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jacksonMessageConverter" />
            </beans:list>
        </beans:property>
    </beans:bean>   

In case it helps, I run the following shell from cygwin for searching for jats

JAR="C:/java/Java/jdk1.7.0_71/bin/jar.exe"
srchdir="c:/appservers/apache-tomcat-7.0.57/webapps/my-web/WEB-INF/lib"

echo "SEARCH FOLDER: $srchdir"
find  $srchdir -print | grep '\.jar$' | while read f
do
    $JAR tf $f | grep 'MappingJackson2HttpMessageConverter\.class' >/dev/null 2>&1
    [ $? -eq 0 ] && echo "==> $f" && $JAR tf $f | grep MappingJackson2HttpMessageConverter\.class
done


回答3:

Normally, the class MappingJackson2HttpMessageConverter exists in spring-web.jar (or spring-web-x.y.z-RELEASE.jar), you should add it to your classpath, or if using maven add following dependency :

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>x.y.z.RELEASE</version>
</dependency>

Edit per Artem Bilan comment :

Normally, if you use maven, it should be present as a dependency of spring-integration-http, provided you have this one.