I have a String property in an object annotated as follows:
@XmlElement(name = "Item", required = true, nillable = true)
private String item;
The result after marshaling is
<Item xsi:nil="true"/>
while I would like it to be
<Item/>
since the third-party service accepting my XML messages wants it like the latter case. I am using jaxb2. Does anyone knows how I could possibly do this?
Thanks a lot
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
The following example requires the use of MOXy as the JAXB provider. This is because the JAXB RI does not call the
XmlAdapter
when the field/property is null. For information on specifying MOXy as your JAXB provider see:StringAdapter
The
XmlAdapter
will convert the String value to an object with a property annotated with@XmlValue
.Root
The
@XmlJavaTypeAdapter
annotation is used to specify theXmlAdapter
:Demo
The following code can be used to demonstrate the above mapping. Two documents are used one with an empty
Item
element, and the other with a populatedItem
element.Output
The following is the output from running the demo code:
For More Information