Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson marshal XMLAttribute gets "null" value

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.

like image 257
ticofab Avatar asked Jul 03 '13 10:07

ticofab


1 Answers

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.

  1. You can annotate your POJO with @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

  2. 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.

like image 141
Pepster Avatar answered Sep 23 '22 21:09

Pepster