如果字段的值为空,如何将Jackson配置为在序列化期间忽略该字段值.

例如:

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; 
}

推荐答案

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,以便在值不为null时显示属性.

更完整的示例在my answerHow to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson中提供.

Java相关问答推荐

将@ManyToOne JPA Composite Key用作Id保存实体添加额外参数

为什么如果数组列表中有重复项,我的代码SOMETIMES不返回true?

Android Studio—java—在onBindViewHolder中,将断点和空白添加到BackclerView中

Java .类参数不通过构造函数传递

为什么JAVA&S清洁器使用链表而不是并发HashSet?

无法了解Java线程所消耗的时间

使用UTC时区将startDatetime转换为本地时间

把一条整型短裤和两条短裤装成一条长的

Java构造函数分支

为什么同步数据块无效?

Java Flux中的延迟增加

如何对存储为字符串的大数字数组进行排序?

无法使用Open WebStart Java 8运行jnlp

为什么没有加载java.se模块?

根据应用程序 Select 的语言检索数据

在单例类上获取Java锁,了解原因

如何在JSP中从select中获取值并将其放入另一个select

MapStruct记录到记录的映射不起作用

如何显示新布局

在JSON上获取反斜杠