我想做以下事情:
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;
}
}
但是这导致以下XML:
<foos>
</foos>
我想要得到的是这样的:
<bars>
<items>
<bar>x</bar>
<bar>y</bar>
</items>
</bars>
<foos>
<items>
<foo>x</foo>
<foo>y</foo>
</items>
</foos>
但是,我能获得该XML的唯一方法是执行以下操作:
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>
{
}
但我不希望抽象类必须知道哪些类正在扩展它。