我有一个超级类型的列表,即
List<IFoo> foo
该列表包含来自两个不同子类型的对象:
public class FooBar implements IFoo{
}
public class FooType implements IFoo{
}
有没有办法向 XmlElement 注释添加多种类型,即
XmlElement(type={FooBar.class, FooType.class)
List<IFoo> foo
我有一个超级类型的列表,即
List<IFoo> foo
该列表包含来自两个不同子类型的对象:
public class FooBar implements IFoo{
}
public class FooType implements IFoo{
}
有没有办法向 XmlElement 注释添加多种类型,即
XmlElement(type={FooBar.class, FooType.class)
List<IFoo> foo
您可以使用 annotation 为列表提供多种类型XmlElements
。
例如
@XmlElements({
@XmlElement(type=FooBar.class),
@XmlElement(type=FooType.class)
})
List<IFoo> foo;
一个集合属性可以包含多种类型。在这个用例中,如果您希望能够往返文档,您将需要某种类型的指示符。
选项1 -@XmlElements
注释采用@XmlElements
注释数组@XmlElement
。这样做的目的是分配一个唯一的元素来标识每种类型。
选项 2 -@XmlElement(type=Object.class)
如果您告诉 JAXB 类型是Object
,它将强制使用属性写出每个值以xsi:type
唯一标识类型。由于不会自动发现值类型,因此您需要继续传递以创建JAXBContext
或引用带有@XmlSeeAlso
注释的 。