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

标题: jackson 的Unrecognized Property Exception错误 [打印本页]

作者: luinstein    时间: 2012-12-13 20:17
标题: jackson 的Unrecognized Property Exception错误
类: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





欢迎光临 创星网络[分享知识 传递快乐] (http://bbs.cxweb.com.cn/) Powered by Discuz! X3