I'd like to do the following:
public abstract class SomeItems<E>
{
protected List<E> items;
public void setItems(List<E> items)
{
this.items = items;
}
....do other stuff with items....
}
@XmlRootElement(name = "foos")
public class Foos extends SomeItems<Foo>
{
@XmlElementWrapper(name="items")
@XmlElement(name="foo")
public List<Foo> getItems()
{
return this.items;
}
}
@XmlRootElement(name = "bars")
public class Bars extends SomeItems<Bar>
{
@XmlElementWrapper(name="items")
@XmlElement(name="bar")
public List<Bar> getItems()
{
return this.items;
}
}
But this results in the following XML:
<foos>
</foos>
What I'm trying to get, is this:
<bars>
<items>
<bar>x</bar>
<bar>y</bar>
</items>
</bars>
<foos>
<items>
<foo>x</foo>
<foo>y</foo>
</items>
</foos>
But, the only way I can get that XML is to do the following:
public abstract class SomeItems<E>
{
protected List<E> items;
public void setItems(List<E> items)
{
this.items = items;
}
@XmlElementWrapper(name="items")
@XmlElements({
@XmlElement(name="foo", type=Foo.class),
@XmlElement(name="bar", type= Bar.class)
})
public List<E> getItems() {
return this.items;
}
}
@XmlRootElement(name = "foos")
public class Foos extends SomeItems<Foo>
{
}
@XmlRootElement(name = "bars")
public class Bars extends SomeItems<Bar>
{
}
But I don't want the abstract class to have to have any knowledge of what classes are extending it.