简体   繁体   中英

JsonInclude annotaion for associated class

By using @JsonInclude(JsonInclude.Include.NON_EMPTY) at class level it ignores null and empty value tags during serialization.

Is there any variant of JsonInclude or any other library or class where we can ignore null and empty properties of associated (has-a) class.

You have the option to do with annotation at the class level, ie:

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

As noted in comments, in versions below 2.x the syntax for this annotation is:

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

The other option is to configure the ObjectMapper directly, simply by calling mapper.setSerializationInclusion(Include.NON_NULL);

As mentioned by drew moore You maybe also can take a look on Ignore Null Fields with Jackson - Baeldung

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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