开发者

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 @XmlJavaTypeAdapters 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
   }

    ...  
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜