Em Ae
Em Ae

Reputation: 8704

Returning value if it's 0

i have following class

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Order {
    private OrderId orderId;
    private int daysElapsed;
    private List<Item> items;
}

Right now, if daysElapsed is 0 it is not included as part of response object. I want it to be there. Is there something provided by jackson which can handle this?

I was thinking to use Integer class and use annotation NON_NULL to exclude it. Is there a better approach to do so?

Upvotes: 0

Views: 290

Answers (1)

davnicwil
davnicwil

Reputation: 30997

You're only including NON_DEFAULT values. The default value of an int field is 0. So when daysElapsed is 0, its value is equal to the default value, so is not included in the JSON.

Just remove the @JsonInclude(JsonInclude.Include.NON_DEFAULT) annotation.

Upvotes: 1

Related Questions