'Java xml serialization with null fields

I have a class with an Integer field and I need to serialize to xml. If the element is null I need to see empty string in the xml. Example:

  <myObj field=""/>

Whatever I tried, I could not achieve this. I tried something like this :

public class MyObj {
    @JacksonXmlProperty(isAttribute = true)
    @JsonInclude(Include.ALWAYS)
    Integer field;
}

How can I achieve that I would be grateful for your help



Solution 1:[1]

As mentioned in the @f1sh comment, empty string ("") is not a valid Integer value.

With Jackson, you can create create a custom serializer (and will need a custom deserializer).

This Baeldung page may help.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DwB