如果某个字段的值为空,如何告诉 Jackson 在序列化期间忽略该字段? [英] How to tell Jackson to ignore a field during serialization if its value is null?
本文介绍了如果某个字段的值为空,如何告诉 Jackson 在序列化期间忽略该字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果该字段的值为空,如何将 Jackson 配置为在序列化期间忽略该字段值.
How can Jackson be configured to ignore a field value during serialization if that field's value is null.
例如:
public class SomeClass {
// what jackson annotation causes jackson to skip over this value if it is null but will
// serialize it otherwise
private String someValue;
}
推荐答案
要使用 Jackson >2.0 禁止序列化具有空值的属性,您可以 直接配置ObjectMapper
,或者利用@JsonInclude
注释:
To suppress serializing properties with null values using Jackson >2.0, you can configure the ObjectMapper
directly, or make use of the @JsonInclude
annotation:
mapper.setSerializationInclusion(Include.NON_NULL);
或:
@JsonInclude(Include.NON_NULL)
class Foo
{
String bar;
}
或者,您可以在 getter 中使用 @JsonInclude
以便在值不为空时显示该属性.
Alternatively, you could use @JsonInclude
in a getter so that the attribute would be shown if the value is not null.
我的回答 如何防止 Map 中的空值和空值通过 Jackson 序列化 bean 中的字段.
这篇关于如果某个字段的值为空,如何告诉 Jackson 在序列化期间忽略该字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文