stefan.stt
stefan.stt

Reputation: 2627

Dynamically ignore json properties based on condition

I have a POJO object with a List<Integer> stores for example. Is it possible to ignore it with @JsonIgrnore based on the condition to be empty or null when returning this object to the frontend?

Upvotes: 1

Views: 2293

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26492

There is annotation called @JsonInclude accepting various configs.

In your case it would be:

@JsonInclude(JsonInclude.Include.NON_EMPTY)
List<Integer> stores

Javadoc says:

NON_EMPTY
Value that indicates that only properties with null value, 
or what is considered empty, are not to be included.

Upvotes: 3

Related Questions