我有一个这样的课程:
@XmlRootElement(name = "PricingGroup")
public class PricingGroup {
...
@XmlAttribute(name = "partyName")
@XmlJavaTypeAdapter(CustomerGroupRelationships.Adapter.class)
private List<BilltoCustomer> billtoCustomers = new ArrayList<BilltoCustomer>();
@XmlAttribute(name = "partyName")
@XmlJavaTypeAdapter(PartyNames.Adapter.class)
private PartyName partyName;
...
}
似乎JAXB无法为一个属性(此处为@XmlJavaTypeAdapter
)映射两个partyName
。如果我注释掉billtoCustomers上的注释或partyName上的注释,则会从XML中读取另一个成员变量而不会出现问题。
如何同时获取这两个值?
答案 0 :(得分:2)
您可以映射其中一个属性(partyName
),然后使用afterUnmarshal
事件派生其他属性(billToCustomers
):
@XmlRootElement(name = "PricingGroup")
public class PricingGroup {
...
@XmlTransient
private List<BilltoCustomer> billtoCustomers = new ArrayList<BilltoCustomer>();
@XmlAttribute(name = "partyName")
@XmlJavaTypeAdapter(PartyNames.Adapter.class)
private PartyName partyName;
void afterUnmarshal(Unmarshaller u, Object parent) {
// Derive billToCustomers from partyName
}
...
}