java.lang.ClassNotFoundException:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

时间:2014-10-01 12:06:31

标签: java spring jackson spring-integration classnotfoundexception

我在我的项目中使用spring-integration。当我在jetty容器中部署我的应用程序时,我遇到以下异常:

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

我在类路径中同时拥有jackson-core-asl-1.8.3jackson-core-2.3.1个罐子。我仍然得到错误。如何解决问题?

3 个答案:

答案 0 :(得分:3)

我也有这个奇怪的错误。我在我的罐子里搜索了Web服务器(tomcat)的webapps / myapp / WEB-INF / lib,这个类就在那里 - 只有一次。

当我在工作中查看另一个项目并使用相同的杰克逊罐时,它就消失了。

最初,我有以下内容:

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

我删除了这些并添加了以下内容(打包到我的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

我在Windows上的Tomcat 7上运行它;使用Java 1.6(用于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>   

如果它有帮助,我从cygwin运行以下shell来搜索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

答案 1 :(得分:2)

有趣的是com.dsths.common.container.ContainerClassLoader ...

spring-integration-http看似ClassLoaderMappingJackson2HttpMessageConverter以及所有其他spring-web类与另一个ClassLoader一起加载。

如果将所有弹簧罐放在同一个类装载器中,那就太棒了。

该应用无法在类路径中找到MappingJackson2HttpMessageConverter。问题是一个类加载器正在选择一个版本而另一个类加载器正在选择另一个版本。

答案 2 :(得分:1)

通常,MappingJackson2HttpMessageConverter(或spring-web.jar)中存在类spring-web-x.y.z-RELEASE.jar,您应该将其添加到类路径中,或者如果使用maven添加以下依赖项:

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

根据Artem Bilan编辑评论:

通常情况下,如果你使用maven,它应该作为spring-integration-http的依赖项存在,前提是你有这个。