繁体   English   中英

关联类的 JsonInclude 注释

[英]JsonInclude annotaion for associated class

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

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

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

 @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);

正如draw moore所提到的,您也许还可以与 Jackson - Baeldung 一起查看Ignore Null Fields

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM