jackson 的UnrecognizedPropertyException错误

前段时间,使用jackson封装了json字符串转换为javabean的方法,代码如下:

Java代码   收藏代码
  1. public static <T> T renderJson2Object(String json, Class clazz){  
  2.        if(!StringUtil.checkObj(json)){  
  3.             return null;  
  4.         }  
  5.         try {  
  6.             mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);  
  7.             DateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  8.             mapper.getSerializationConfig().setDateFormat(myDateFormat);  
  9.             return (T)mapper.readValue(json, clazz);  
  10.         } catch (IOException e) {  
  11.             log.error(e);  
  12.             throw new IllegalArgumentException(e);  
  13.         }  
  14.     }  

 ,这个方法可以根据json字符串,转换为clazz指定的类型的对象,如:

Java代码   收藏代码
  1. renderJson2Object("{\"name\":\"china\",\"age\":\"<span style="white-space: normal;">5000</span>\"}",Student.class);  

Student类里有name,age的get,set方法,代码如下:

 

Java代码   收藏代码
  1. public class Student implements Serializable{  
  2.     private static final long serialVersionUID = 685922460589405829L;  
  3.   
  4.     private String name;  
  5.     private String age;  
  6.   
  7.    /*get set略*/  
  8. }  

 

根据上面的json字符串可以正常转换为Student对象,

但是通常情况下,从前端传json字符串到后端,json字符串的值是不可控的或者被框架修改了json字符串,如在里面添加了其他的键值对,

如现在的json字符串为:"{\"address\":\"hunan\",\"name\":\"china\",\"age\":\"5000\"}",

Student类里根本没有address属性,这样的情况下使用renderJson2Object方法时,会抛

Java代码   收藏代码
  1. java.lang.IllegalArgumentException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "address" (Class util.Student), not marked as ignorable  

  意思是说Student类里没有address这个属性,所以无法正常转化,同时还指明了not marked as ignorable,即没有标明可忽略的特性,先看源码这句话的理解这句话的意思

类:org.codehaus.jackson.map.deser.BeanDeserializer中的

 

Java代码   收藏代码
  1. @Override  
  2.     protected void handleUnknownProperty(JsonParser jp, DeserializationContext ctxt, Object beanOrClass, String propName)  
  3.         throws IOException, JsonProcessingException  
  4.     {  
  5.         ... ... ...  
  6.         <span style="color: #ff0000;">// If registered as ignorable, skip</span>  
  7.         <span style="color: #ff0000;">if (_ignoreAllUnknown ||  
  8.             (_ignorableProps != null && _ignorableProps.contains(propName))) {  
  9.             jp.skipChildren();  
  10.             return;  
  11.         }</span>  
  12.          ... ... ...  
  13.     }  

    源码注释说,如果注册了忽略特性,则会跳过此步骤,那到底需要怎么忽略呢?

请再看类:org.codehaus.jackson.map.deser.BeanDeserializerFactory中的

 

Java代码   收藏代码
  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类的正确做法为:

 

Java代码   收藏代码
  1. @JsonIgnoreProperties(ignoreUnknown = true
  2. public class Student implements Serializable{  
  3.     private static final long serialVersionUID = 685922460589405829L;  
  4.   
  5.     private String name;  
  6.     private String age;  
  7.   
  8.    /*get set.....*/  
  9. }  
 看红色注解,现在暂时找到在类中添加注解(感觉具体的pojo对象和jackson耦合),不知道有没有其他方法,设全局变量来控制,如果有朋友知道,请告诉我谢谢。。。

使用mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false),


在对应的javabean vo上加注解:

@JsonIgnoreProperties(ignoreUnknown = true) 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值