I'm using MOXy 2.5.1 along with Jersey 2.4 and Hibernate. I'm seeing a strange marshalling behavior with MOXy with a specific set of classes.
I have classes that look similar to this (with additional stuff removed for brevity):
public abstract class ListWrapper<M> {
protected List<M> records = new ArrayList<M>();
public abstract List<M> getRecords();
// plus functions to make it look like a List / Iterator
}
public abstract class RecordList<T extends Record> extends ListWrapper<T> {
protected Person person;
@XmlElement
public Person getPerson() { return person; }
}
public class FooList extends RecordList<Foo> {
@Override
@XmlElement(type=Foo.class)
public List<Foo> getRecords() { return super.records; }
}
public class Holder {
@XmlElement
public FooList getFooList() { /* ... */ }
}
When Holder
gets marshalled to XML using MOXy I get the following:
<holder>
<fooList>
<records>...</records>
<records>...</records>
<person>...</person>
</fooList>
</holder>
I was expecting those <records>
elements to be named <foos>
instead.
If I put a name="foos"
entry on the @XmlElement
declaration in FooList
I will get my expected name, but then MOXy does something else that's a bit strange. It will still output elements named <records>
within the <fooList>
element, but these <records>
will only contain the <id>
values for the (Hibernate) database entries that these correspond to. And so it looks like this:
<holder>
<fooList>
<records><id>5</id></records>
<records><id>10</id></records>
<person>...</person>
<foos>...</foos>
<foos>...</foos>
</fooList>
</holder>
It doesn't matter if I use an @XmlElementWrapper
, a @XmlJavaTypeAdapter
, or any other thing I can think of. I will either get the wrong names, or a second set of elements that contain a limited set of data. It's almost as though MOXy is not understanding the inheritance structure here or something.
I'll play around with a few other things in the meantime (e.g. ListWrapper
API; it's not my design), but to me this looks like a bug in MOXy. Note that the same behaviors happen with JSON, so this isn't limited to XML.
I found a solution. I simply put
@XmlAccessorType(XmlAccessType.NONE)
on theListWrapper
andRecordList
classes (it was already on theFooList
class in my code). I think MOXy is (incorrectly?) looking up the tree toListWrapper
and creating an output entry based on that.