之前在spring 3中用的好好的MappingJacksonHttpMessageConverter转换器,将对象转为json响应给浏览器,在spring4中,报错。
Cannot create inner bean 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#0'
of type [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter] while setting bean
property 'messageConverters' with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#0': Failed to
introspect bean class [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter] for lookup method metadata:
could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError:
com/fasterxml/jackson/core/util/DefaultPrettyPrinter$Indente
解决:
这种错误,是因为 spring4开始,使用了 MappingJackson2HttpMessageConverter替换之气的MappingJacksonHttpMessageConverter,
同时,依赖的jar包也换了。从jackson-mapper-asl,jackson-core-asl,换为了 jackson-core,jackson-databind
springmvc配置文件:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<!-- <constructor-arg ref="utf8Charset"/> -->
<!-- <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /> -->
<property name="writeAcceptCharset" value="false" /><!-- 用于避免响应头过大 -->
<property name = "supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<!-- <value>text/json;charset=UTF-8</value> -->
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
参考:
springmvc和json整合配置方法
解决spring4.x中的MappingJacksonHttpMessageConverter错误
spring3.X升级到spring4.X时关于MappingJacksonHttpMessageConverter的问题