设为首页收藏本站
开启辅助访问
切换到宽版

创星网络[分享知识 传递快乐]

 找回密码
 立即注册

QQ登录

只需一步,快速开始

用新浪微博登录

只需一步,快速搞定

搜索
查看: 4971|回复: 0
打印 上一主题 下一主题

jackson 的Unrecognized Property Exception错误

[复制链接]

我玩的应用:

跳转到指定楼层
楼主
发表于 2012-12-13 20:17:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
类:org.codehaus.jackson.map.deser.BeanDeserializer中的
  1. @Override
  2.     protected void handleUnknownProperty(JsonParser jp, DeserializationContext ctxt, Object beanOrClass, String propName)
  3.         throws IOException, JsonProcessingException
  4.     {
  5.         ... ... ...
  6.         // If registered as ignorable, skip
  7.         if (_ignoreAllUnknown ||
  8.             (_ignorableProps != null && _ignorableProps.contains(propName))) {
  9.             jp.skipChildren();
  10.             return;
  11.         }
  12.          ... ... ...
  13.     }
复制代码
源码注释说,如果注册了忽略特性,则会跳过此步骤,那到底需要怎么忽略呢?
请再看类:org.codehaus.jackson.map.deser.BeanDeserializerFactory中的
  1. protected void addBeanProps(DeserializationConfig config,
  2.             BasicBeanDescription beanDesc, BeanDeserializerBuilder builder)
  3.         throws JsonMappingException
  4.     {
  5.         ... .... ...
  6.         // Things specified as "ok to ignore"? [JACKSON-77]
  7.         AnnotationIntrospector intr = config.getAnnotationIntrospector();
  8.         boolean ignoreAny = false;
  9.         {
  10.             Boolean B = intr.findIgnoreUnknownProperties(beanDesc.getClassInfo());
  11.             if (B != null) {
  12.                 ignoreAny = B.booleanValue();
  13.                 builder.setIgnoreUnknownProperties(ignoreAny);
  14.             }
  15.         }
  16.      ... ... ...
  17. }
复制代码
intr.findIgnoreUnknownProperties(beanDesc.getClassInfo());

会查找目标对象中,是否使用了JsonIgnoreProperties 注解,其中把注解的value值赋给了builder.setIgnoreUnknownProperties(ignoreAny);

到此Student类的正确做法为:
  1. @JsonIgnoreProperties(ignoreUnknown = true)
  2. public class Student implements Serializable{
  3.     private static final long serialVersionUID = 685922460589405829L;

  4.     private String name;
  5.     private String age;

  6.    /*get set.....*/
复制代码
看注解@JsonIgnoreProperties(ignoreUnknown = true),现在暂时找到在类中添加注解(感觉具体的pojo对象和jackson耦合),不知道有没有其他方法,设全局变量来控制,如果有朋友知道,请告诉我谢谢。。。

from:http://go.cxweb.com.cn/tk-m5
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|创星网络 ( 苏ICP备11027519号|网站地图  

GMT+8, 2024-5-2 15:29 , Processed in 0.083283 second(s), 28 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表