如果值为null,如何告诉Jackson在序列化期间忽略某个字段? [英] How to tell Jackson to ignore a field during serialization if its value is null?

查看:197
本文介绍了如果值为null,如何告诉Jackson在序列化期间忽略某个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果该字段的值为null,如何将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; 
}


推荐答案

禁止使用null序列化属性使用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;
}

或者,您可以使用 @JsonInclude 在一个getter中,如果该值不为null,则显示该属性。

Alternatively, you could use @JsonInclude in a getter so that the attribute would be shown if the value is not null.

我的回答 如何防止Map中的空值和bean内的空字段通过Jackson序列化

这篇关于如果值为null,如何告诉Jackson在序列化期间忽略某个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆