I want XML like this:
<simple>Foo</simple>
I can do this successfully via a JAXB class that looks like this:
@XmlRootElement(name="simple")
class Simple {
@XmlValue
public String contents;
}
But now I need to make the Simple class be a subclass of another class like so:
@XmlRootElement(name="simple")
class Simple extends OtherClass {
@XmlValue
public String contents;
}
That fails with @XmlValue is not allowed on a class that derives another class. I can’t easily refactor the superclass away (because of the way we’re using @XmlElementRef on a wrapper class). Is there a workaround that will let me annotate my subclass to generate that simple XML?
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
This use case is supported by MOXy, and IMHO should be supported by the JAXB RI as well:
Simple
This class has a field mapped with
@XmlValueand extendsOtherClass:OtherClass
This is the super class. In MOXy the subclass can map a field/property with
@XmlValueas long as the super class does not have any mappings to an XML element:Demo
Output
For More Information on Specifying MOXy as Your JAXB Provider