Mapping Java collections which contains super- and sub-types with JAXB

后端 未结 1 533
梦谈多话
梦谈多话 2021-01-15 09:35

I\'m trying to produce something like this with JAXB:

  
    Foo
    Bar
           


        
相关标签:
1条回答
  • 2021-01-15 10:13

    Simply modify your example to use the @XmlElementRef annotation on the identities property.

    import java.util.HashSet;
    import java.util.Set;
    
    import javax.xml.bind.annotation.XmlElementRef;
    import javax.xml.bind.annotation.XmlElementWrapper;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name = "person")
    public class Person {
        public String firstName; 
        public String lastName;
        @XmlElementWrapper(name = "identities")
        @XmlElementRef
        public Set<Identity> identities = new HashSet<Identity>();
    }
    
    0 讨论(0)
提交回复
热议问题