jackson使用总结

在使用jackson时报错:java.lang.IllegalArgumentException: No converter found for return value of type: class X.X.X。

错误原因:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) 由于项目中使用了这个注解

这是注解是spring MVC 3.X 支持注解,在需要序列化为json输出的类上增加的,我的spring版本是4.2.4所以报错。

修改方法:

1. springmvc.xml中添加代码:

    <bean  
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="messageConverters">  
            <list>  

                <!-- <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 

                  jackson1.x版本使用-->

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

    </bean>  

或者 添加下面这个(我项目中使用的):

<mvc:annotation-driven />

2. 导入jar包:


3. 相关类上使用注解(我这里使用的是dto封装的一个类):

我这里使用了两个相关的注解:

@JsonInclude(JsonInclude.Include.NON_NULL)

作用:返回给前端的json中忽略为null的相关数据

@JsonIgnore 

作用:返回给前端的json中忽略相关的属性

在class上使用


在属性上使用



引入的jar包是:


通过以上修改测试成功返回json。

知识点总结:

1.spring版本,3.x和4.x的jackson包不一样

spring3.x是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

spring4.x是org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

2. jackson使用时1.x和2.x的包对应的spring版本不一样,1.x对应spring3.x,2.x对应spring4.x

相关链接:http://blog.youkuaiyun.com/sinat_26630143/article/details/77451801

                 http://blog.youkuaiyun.com/limm33/article/details/52811499

                 http://blog.youkuaiyun.com/vili_sky/article/details/73105550





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值