jackson 的Unrecognized Property Exception错误
类:org.codehaus.jackson.map.deser.BeanDeserializer中的@Overrideprotected void handleUnknownProperty(JsonParser jp, DeserializationContext ctxt, Object beanOrClass, String propName)
throws IOException, JsonProcessingException
{
... ... ...
// If registered as ignorable, skip
if (_ignoreAllUnknown ||
(_ignorableProps != null && _ignorableProps.contains(propName))) {
jp.skipChildren();
return;
}
... ... ...
}源码注释说,如果注册了忽略特性,则会跳过此步骤,那到底需要怎么忽略呢?请再看类:org.codehaus.jackson.map.deser.BeanDeserializerFactory中的protected void addBeanProps(DeserializationConfig config,
BasicBeanDescription beanDesc, BeanDeserializerBuilder builder)
throws JsonMappingException
{
... .... ...
// Things specified as "ok to ignore"?
AnnotationIntrospector intr = config.getAnnotationIntrospector();
boolean ignoreAny = false;
{
Boolean B = intr.findIgnoreUnknownProperties(beanDesc.getClassInfo());
if (B != null) {
ignoreAny = B.booleanValue();
builder.setIgnoreUnknownProperties(ignoreAny);
}
}
... ... ...
}intr.findIgnoreUnknownProperties(beanDesc.getClassInfo());
会查找目标对象中,是否使用了JsonIgnoreProperties 注解,其中把注解的value值赋给了builder.setIgnoreUnknownProperties(ignoreAny);
到此Student类的正确做法为:@JsonIgnoreProperties(ignoreUnknown = true)
public class Student implements Serializable{
private static final long serialVersionUID = 685922460589405829L;
private String name;
private String age;
/*get set.....*/
}看注解@JsonIgnoreProperties(ignoreUnknown = true),现在暂时找到在类中添加注解(感觉具体的pojo对象和jackson耦合),不知道有没有其他方法,设全局变量来控制,如果有朋友知道,请告诉我谢谢。。。
from:http://go.cxweb.com.cn/tk-m5
页:
[1]