问题描述
我通常使用objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL),因为我从不希望序列化我的类的空值。除了现在我应该写出一个特定的字段,即使它是null。我可以在这一个字段上添加一个快速注释来覆盖该字段的Inclusion.NON_NULL属性吗?有什么好办法实现这个目标?
I normally use objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL) because I never want the null values of my classes serialized. Except now I have a specific field should be written out, even if it is null. Is there a quick annotation I can put on this one field that overrides the Inclusion.NON_NULL property for that one field? What's a good way to achieve this?
推荐答案
使用Jackson 1.x你可以使用 @JsonSerialize( include = Include.ALWAYS)
和Jackson 2.x你可以使用 @JsonInclude(Include.ALWAYS)
。这些注释将覆盖 ObjectMapper
中的默认配置。
With Jackson 1.x you can use @JsonSerialize(include=Include.ALWAYS)
and with Jackson 2.x you can use @JsonInclude(Include.ALWAYS)
. These annotations will override the default config from your ObjectMapper
.
这篇关于杰克逊使用JsonSerialize.Inclusion.NON_NULL除了一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!