JsonInclude注释相关类

问题描述

通过在类级别使用@JsonInclude(JsonInclude.Include.NON_EMPTY),它会在序列化过程中忽略null和空值标签

是否存在JsonInclude的任何变体或任何其他库或类,在其中我们可以忽略关联的(has-a)类的null和empty属性

解决方法

您可以选择在类级别使用注释,即:

@JsonInclude(Include.NON_NULL) //or Include.NON_EMPTY,if that fits your use case 
public static class Request {
  // ...
}

如注释中所述,在2.x以下的版本中,此注释的语法为:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) // or JsonSerialize.Inclusion.NON_EMPTY

另一种选择是直接配置ObjectMapper,只需调用 mapper.setSerializationInclusion(Include.NON_NULL);

drew moore所述 您也许还可以看看Ignore Null Fields with Jackson - Baeldung