原创

MappingJacksonHttpMessageConverter与MappingJackson2HttpMessageConverter区别

        最近公司搭建cas单点登录框架,需要客户端本人采用的框架是Springmvc+mybatis+cas+shiro,shiro权限控制,下面本站素文宅www.yoodb.com分享一下MappingJacksonHttpMessageConverter与MappingJackson2HttpMessageConverter在使用时遇到的异常问题,仅供大家参考学习。


使用spring3.1.1版本抛出异常信息,具体如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.
RequestMappingHandlerAdapter#0' defined in class path resource [conf/spring-mvc.xml]: Cannot create inner bean 'org.springframework.
http.converter.json.MappingJackson2HttpMessageConverter#7d0e6cbd' of type [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter] 
while setting bean property 'messageConverters' with key [1]; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: 
Cannot find class [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter] for bean with name '
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#7d0e6cbd' defined in class path resource [conf/spring-mvc.xml];
 nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:281)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:120)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:153)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
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 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5118)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5634)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)


配置文件的配置信息跟jackson的版本有关,具体如下两种版本:

版本一: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

版本二:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

Spring4.*版本Jackson1.*(测试1.9.13版)版本包,配置文件如下:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
    <property name="supportedMediaTypes">    
        <list>    
            <value>application/json;charset=UTF-8</value>    
       </list>    
    </property>  
</bean>  
</list>
</property>
</bean>


Spring4.*版本Jackson2.*包(测试2.5.1版),配置文件如下:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">    
    <property name="supportedMediaTypes">    
        <list>    
            <value>application/json;charset=UTF-8</value>    
       </list>    
    </property>  
</bean>  
</list>
</property>
</bean>

关注下方微信公众号“Java精选”(w_z90110),回复关键字领取资料:如HadoopDubboCAS源码等等,免费领取资料视频和项目。 

涵盖:程序人生、搞笑视频、算法与数据结构、黑客技术与网络安全、前端开发、Java、Python、Redis缓存、Spring源码、各大主流框架、Web开发、大数据技术、Storm、Hadoop、MapReduce、Spark、elasticsearch、单点登录统一认证、分布式框架、集群、安卓开发、iOS开发、C/C++、.NET、Linux、Mysql、Oracle、NoSQL非关系型数据库、运维等。

相关推荐

评论

  1. #1

    懒人 (2017/07/26 12:26:32)回复
    谢谢,昨天弄了一下也没搞明白是怎么回事,现在终于明白是什么原因导致MappingJackson2HttpMessageConverter异常了。

分享:

支付宝

微信