I have an issue with JAXB / Jackson marshalling. I have such an annotation
@XmlAttribute(name = "private")
protected Boolean mPrivate;
and I expect that this attribute be absent if the mPrivate
variable is null
.
This works fine if the output is XML. But if I switch to JSON, using Jackson, the output is
xxxxxxx, "private":null, xxxxxxxx
Anybody has an idea why this happens and how to fix it? Thanks in advance.
Jackson is compatible with the JAXB annotations. Hence JAXB doesn't support default values for XmlAttributes as the default behavior is to leave them out if value is null when serializing to XML.
There are a few options to achieve this for JSON.
You can annotate your POJO with @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
You can set the default behavior of the ObjectMapper to exclude null-values from serialisation. You do so by calling:
setSerializationInclusion(Inclusion.NON_NULL);
...on the ObjectMapper instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With