public class User {
private int id;
private Date birthday;
private double money;
private String name;
}
{"id":1,"birthday":1365512156293,"money":0.0,"name":"JACK","age": 15}
当反序列化时 有多余的属性(age)会报错
//通过如下配置解决
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(DeserializationContext ctxt,
JsonParser jp, JsonDeserializer<?> deserializer,
Object beanOrClass, String propertyName)
throws IOException, JsonProcessingException {
if(propertyName == "age") {
return true;
}
return false;
}
});
//也可以通过这种方式进行全局设置,在反序列化时会把user当中没有的属性(age)传递进来进行处理
//或者
@JsonAnySetter
public void handleUnknown(String key, Object value) {
// do something: put to a Map; log a warning, whatever
System.out.println(key + "---" + value.getClass());
}
//也可以用注释对具体的类进行设置
@JsonIgnoreProperties(ignoreUnknown=true)
public class User