Two @XmlJavaTypeAdapters for one @XmlAttribute in JAXB?
I have a class like this:
@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;
...
}
It seems JAXB can't map two @XmlJavaTypeAdapter
s for one attribute (here partyName
). If I comment out either the a开发者_C百科nnotations on billtoCustomers or the annotations on partyName, the other member variable is read from XML without problems.
How can I get both values at the same time?
You could map one of the properties (partyName
) and then use an afterUnmarshal
event to derive the other property (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
}
...
}
精彩评论