public abstract class Parent<T> {
protected List<T> list;
@XmlTransient //Question why do we have to give this here?
public abstract List<T> getList();
public abstract void setList(List<T> list);
}
@XmlRootElement(name = "child1")
class Child1 extends Parent<ExtendedElement1>{
@Override
public void setList(List<ExtendedElement1> list){
this.list = list;
}
@Override
@XmlElementWrapper(name = "child1-list")
@XmlElement(name = "child-list-element")
public List<ExtendedElement1> getList(){
return this.list;
}
}
@XmlRootElement(name = "child2")
class Child2 extends Parent<ExtendedElement2>{
@Override
public void setList(List<ExtendedElement2> list){
this.list = list;
}
@Override
@XmlElementWrapper(name = "child1-list")
@XmlElement(name = "child-list-element")
public List<ExtendedElement2> getList(){
return this.list;
}
}
class Element{
@XmlElement(name = "integer", type = int.class)
private int i = 2;
}
class ExtendedElement1 extends Element{
@XmlElement(name = "extended-element1-str", type = String.class)
private String str = "hello";
}
class ExtendedElement2 extends Element{
@XmlElement(name = "extended-element2-str", type = String.class)
private String str1 = "hello_there";
}
正如我在示例中所示,当我<{>>删除 @XmlTransient
类Parent
方法中的getList()
时,xml被编组:
<child1>
<!-- List is serialized 2 times -->
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="extendedElement1">
<integer>2</integer>
<extended-element1-str>hello</extended-element1-str>
</list>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="extendedElement1">
<integer>2</integer>
<extended-element1-str>hello</extended-element1-str>
</list>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="extendedElement1">
<integer>2</integer>
<extended-element1-str>hello</extended-element1-str>
</list>
<child1-list>
<child-list-element>
<integer>2</integer>
<extended-element1-str>hello</extended-element1-str>
</child-list-element>
<child-list-element>
<integer>2</integer>
<extended-element1-str>hello</extended-element1-str>
</child-list-element>
<child-list-element>
<integer>2</integer>
<extended-element1-str>hello</extended-element1-str>
</child-list-element>
</child1-list>
</child1>
但是当我添加 @XmlTransient
注释时,就像在示例中一样,xml根据需要仅按 ONE 列表进行序列化。
<child1>
<child1-list>
<child-list-element>
<integer>2</integer>
<extended-element1-str>hello</extended-element1-str>
</child-list-element>
<child-list-element>
<integer>2</integer>
<extended-element1-str>hello</extended-element1-str>
</child-list-element>
<child-list-element>
<integer>2</integer>
<extended-element1-str>hello</extended-element1-str>
</child-list-element>
</child1-list>
</child1>
所以请有人解释一下,为什么需要在父类getter方法中给出@XmlTransient
?对于这些情况,继承和JAXB如何相互关联?
答案 0 :(得分:8)
为什么会发生
JAXB (JSR-222)实现会将其知道的每个域对象映射到复杂类型。这意味着它认为Parent
类存在以下XML类型(list
不是@XmlTransient
时)。
<xs:complexType name="parent" abstract="true">
<xs:sequence>
<xs:element name="list" type="xs:anyType" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
现在Child2
也有一个复杂的类型。 JAXB可以完成两件事:
child2
类型扩展了parent
。这意味着它从parent
类型加上它自己的所有元素。 <xs:complexType name="child2">
<xs:complexContent>
<xs:extension base="parent">
<xs:sequence>
<xs:element name="child1-list" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="child-list-element" type="extendedElement2" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
如何修复
您可以将@XmlTransient
放在list
课程的Parent
媒体资源上,但我建议您使用Parent
注释@XmlTransient
课程。
import java.util.List;
import javax.xml.bind.annotation.XmlTransient;
@XmlTransient
public abstract class Parent<T> {
protected List<T> list;
public abstract List<T> getList();
public abstract void setList(List<T> list);
}
这会将其作为映射类删除,与Child2
对应的复杂类型将变为:
<xs:complexType name="child2">
<xs:sequence>
<xs:element name="child1-list" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="child-list-element" type="extendedElement2" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>