Is it possible to use @XmlValue annotation on a referenced object annotated with @XmlElementRef? The goal is to produce the following XML output:
<foo>
<bar>Blah</bar>
</foo>
Given the following example JAXBContext initialization is throwing a NPE:
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:128)
Foo Class
@XmlRootElement(name = "foo")
public class Foo
{
@XmlElementRef
private Bar bar;
}
Bar Class
@XmlRootElement(name = "bar")
public class Bar extends BarBase // BarBase is annotated with @XmlTransient
{
@XmlValue
private String value;
}
Is there any way to achieve the desired output based on the implementation? I’ve been looking into Adapters, but haven’t been successful at implement one. Thanks in advance!
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
JAXB RI Issue Confirmed
I have been able to confirm the issue you are seeing with the JAXB RI. I get the following stack trace:
This is a bug in the JAXB RI and I would recommend opening a ticket at the following link:
Option #1 – Use an Alternate Mapping with JAXB RI
The following mapping appears to work with the JAXB RI (using
@XmlElementinstead of@XmlElementRef.Option #2 – Use an Alternate JAXB (JSR-222) Provider
Your mappings are correct. If you use another JAXB (JSR-222) provider such as EclipseLink MOXy you will not get this exception. Below is a link that explains how to use MOXy as your JAXB provider: