Jackson marshal XMLAttribute gets "null" value

annotations, jackson, jaxb, xml-attribute

Solution

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.

Problem

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.

Original source

Related problems