Spring mvc将对象转换为json返回的配置

之前在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">
                
                 
                 <property name="writeAcceptCharset" value="false" /> 
                 <property name = "supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8value>   
                        <value>text/html;charset=UTF-8value>   

                    list>   
                 property>
            bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                 <property name = "supportedMediaTypes">
                    <list>
                        <value>text/json;charset=UTF-8value>   
                    list>   
                 property>
                
            bean>
        mvc:message-converters>
    mvc:annotation-driven>

你可能感兴趣的:(Spring,JSON)