Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JAXB映射包含超类型和子类型的Java集合_Java_Collections_Jaxb - Fatal编程技术网

使用JAXB映射包含超类型和子类型的Java集合

使用JAXB映射包含超类型和子类型的Java集合,java,collections,jaxb,Java,Collections,Jaxb,我正试图用JAXB制作类似的东西: <person> <firstName>Foo</firstName> <lastName>Bar</lastName> <identities> <green id="greenId"> <some_elements.... </green> <blue id="b

我正试图用JAXB制作类似的东西:

  <person>
    <firstName>Foo</firstName>
    <lastName>Bar</lastName>
    <identities>
      <green id="greenId">
            <some_elements....
      </green>
      <blue id="blueId"/>
    </identities>
如何正确注释类以获得所需的内容?目前,输出如下:

<identities>
    <identities id="0815"/>
</identities>

只需修改示例,在Identifications属性上使用@XmlElementRef注释即可

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>();
}
import java.util.HashSet;
导入java.util.Set;
导入javax.xml.bind.annotation.xmlementref;
导入javax.xml.bind.annotation.XmlElementWrapper;
导入javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name=“person”)
公共阶层人士{
公共字符串名;
公共字符串lastName;
@XmlElementWrapper(name=“identifications”)
@xmlementref
公共集标识=新HashSet();
}
<identities>
    <identities id="0815"/>
</identities>
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>();
}