When marshaling an object tree I am making use of the @XmlJavaTypeAdapter
. Some adapters return objects of classes which themselves have the @XmlJavaTypeAdapter
annotation. This worked fine when I used the JAXB implementation packaged with websphere 7, but when I use org.eclipse.persistence.jaxb.JAXBContextFactory
the @XmlJavaTypeAdapter
annotations on the objects returned by the first adapter are ignored. Is this a known issue, or am I doing something wrong?
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class,C.class);
System.out.println(jc.getClass());
Root root = new Root();
A a = new A();
root.a = a;
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
Root
@XmlRootElement
public class Root {
@XmlJavaTypeAdapter(AAdapter.class)
public A a;
}
A
public class A {
public B b = new B();
}
B
public class B {
@XmlJavaTypeAdapter(GAdapter.class)
@XmlElement(name="b")
public G<C> gc = new G<C>();
public B(){
gc.t = new C();
}
}
C
public class C {
public String c = "Foo";
}
G
public class G<T> {
T t;
}
Then the adapter for A
...
public class AAdapter extends XmlAdapter<B, A> {
@Override
public A unmarshal(B b) throws Exception {
A a = new A();
a.b = b;
return a;
}
@Override
public B marshal(A a) throws Exception {
return a.b;
}
}
And the adapter for the generic type
public class GAdapter<T> extends XmlAdapter<T, G<T>> {
@Override
public G<T> unmarshal(T c) throws Exception {
return new G<T>();
}
@Override
public T marshal(G<T> g) throws Exception {
return g.t;
}
}
when marshaled with class com.sun.xml.bind.v2.runtime.JAXBContextImpl
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<a>
<b xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c">
<c>Foo</c>
</b>
</a>
</root>
when marshaled with org.eclipse.persistence.jaxb.JAXBContext
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a>
<b>forum237.C@23752375</b>
</a>
</root>
I think the issue is with generic types. The goal is to skip the generic type from being marshaled, and only marshal T
, as well as process T
’s annotations, if any.