在java中使用jackson json反序列化时忽略缺少的属性 [英] Ignore missing properties during jackson json deserialize in java
本文介绍了在java中使用jackson json反序列化时忽略缺少的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在示例中
Class Person{
String name;
int age;
}
如果json对象缺少属性'age',
If the json object has a missing property 'age',
{
name : John
}
Person person = objectMapper.readValue(jsonFileReader, Person.class);
它会抛出一个JsonMappingException,说它不能反序列化。是否有注释在反序列化期间忽略缺失的字段?
it throws a JsonMappingException saying it cannot deserialize. Is there an annotation to ignore missing fields during deserialization ?
谢谢
推荐答案
我认为你想要的是
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Person {
...
}
这是Jackson 1.x的方式。我认为在2.x中有一种新的方式。类似
that's the Jackson 1.x way. I think there's a new way in 2.x. Something like
@JsonInclude(Include.NON_NULL)
public class Person {
...
}
这些将告诉Jackson只序列化非空值,不要抱怨反序列化缺失值时。我认为它只会将其设置为Java默认值。
These will tell Jackson to only serialize values that are not null, and don't complain when deserializing a missing value. I think it will just set it to the Java default.
这篇关于在java中使用jackson json反序列化时忽略缺少的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文