开发者

Two @XmlJavaTypeAdapters for one @XmlAttribute in JAXB?

开发者 https://www.devze.com 2023-03-10 13:29 出处:网络
I have a class like this: @XmlRootElement(name = \"PricingGroup\") public class PricingGroup { ... @XmlAttribute(name = \"partyName\")

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

精彩评论

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

关注公众号