Using JAXB @XmlValue with @XmlElementRef
Can I use @XmlValue annotation on a link object annotated with @XmlElementRef? The goal is to generate the following XML output:
<foo>
<bar>Blah</bar>
</foo>
Given the following example, JAXBContext initialization throws 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 a way to achieve the desired result based on implementation? I've looked into adapters but haven't been able to implement them. Thank you in advance!
source to share
Note. I am EclipseLink JAXB (MOXy) and a member of the JAXB team (JSR-222) .
JAXB RI error
I was able to confirm the problem you are seeing with JAXB RI. I am getting the following stack trace:
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:166)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:494)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:311)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:126)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1148)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:445)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at forum14490548.Demo.main(Demo.java:10)
This is a bug in JAXB RI and I would recommend opening a ticket from the following link:
Option # 1 - Using Alternate Mapping with JAXB RI
The following mapping to RI JAXB appears (using @XmlElement
instead of @XmlElementRef
.
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "foo")
public class Foo
{
@XmlElement
private Bar bar;
}
Option # 2 - Using an alternative JAXB provider (JSR-222)
Your mappings are correct. If you are using another JAXB (JSR-222) like EclipseLink MOXy, you will not get this exception. Below is a link that explains how to use MOXy as a JAXB provider:
source to share
I'm here for exactly the same problem. This question is still alive with 1.8.0_77
.
I got around with an extra dummy type attribute as the quality user3345125
.
@XmlAttribute
public String getNothing() {
return null;
}
For someone I would like to trace. https://github.com/javaee/jaxb-v2/issues/1027
source to share