Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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
Interface JAXB注释-映射接口和@XmlElementWrapper_Interface_Annotations_Jaxb - Fatal编程技术网

Interface JAXB注释-映射接口和@XmlElementWrapper

Interface JAXB注释-映射接口和@XmlElementWrapper,interface,annotations,jaxb,Interface,Annotations,Jaxb,对于一个字段的JAXB注释,我遇到了麻烦,该字段是一个列表,其泛化类型是一个接口。当我声明如下时: @XmlAnyElement private List<Animal> animals; @xmlanyement 私人名单 @XmlRootElement 班级动物园{ @XmlElementWrapper @xmlanyement(lax=true) 私人动物名录; 公共静态void main(字符串[]args)引发异常{ 动物园=新动物园(); zoo.anies=新Arra

对于一个字段的JAXB注释,我遇到了麻烦,该字段是一个列表,其泛化类型是一个接口。当我声明如下时:

@XmlAnyElement
private List<Animal> animals;
@xmlanyement
私人名单

@XmlRootElement
班级动物园{
@XmlElementWrapper
@xmlanyement(lax=true)
私人动物名录;
公共静态void main(字符串[]args)引发异常{
动物园=新动物园();
zoo.anies=新ArrayList();
动物园。动物。添加(新狗());
动物园。动物。添加(新猫());
JAXBContext jc=JAXBContext.newInstance(Zoo.class、Dog.class、Cat.class);
Marshaller=jc.createMarshaller();
ByteArrayOutputStream os=新建ByteArrayOutputStream();
马歇尔(动物园,os);
System.out.println(os.toString());
Unmarshaller Unmarshaller=jc.createUnmarshaller();
Zoo unmarshalledZoo=(Zoo)unmarshaller.unmarshal(新的ByteArrayInputStream(os.toByteArray());
if(unmarshalledZoo.animals==null){
System.out.println(“动物为空”);
}else if(unmarshalledZoo.animals.size()==2){
System.out.println(“它成功了”);
}否则{
System.out.println(“失败!”);
}
}
公共接口动物{}
@XmlRootElement
公共静态类Dog实现了Animal{}
@XmlRootElement
公共静态类Cat实现了Animal{}
} 

当我使用jdk1.6.0_20运行您的测试程序时,它似乎正常工作,我得到以下输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<zoo><animals><dog/><cat/></animals></zoo>
it worked

它起作用了

您是否尝试过在附件中添加注释?我以前在@XmlElementWrapper中也遇到过这个问题,但是我通过注释我的getter而不是注释字段声明来解决了这个问题。

应该使用 @XmlElementRefs({ @XmlElementRef(类型=Dog.class), @XmlElementRef(type=Cat.class)}) 私人动物名录

或使用
@仅XmlAnyElement(lax=true),并将Dog.class、Cat.class添加到JaxbContext

当我使用jdk1.6.020运行测试程序时,它不起作用,但是一旦我将列表的注释从
@XmlAnyElement(lax=true)
更改为
@xmlementref({@xmlementref(type=Dog.class),@xmlementref(type=Cat.class)})
然后它就可以工作了。是否将
Dog.class
Cat.class
添加到JAXBContext并不重要。

这是JAXB 2.1.13中修复的问题。更新您的库或使用jdk1.7或更高版本,问题就会得到解决。

我使用的是jdk1.6.020附带的JAXB 2.1版本。您还可以使用MOXy JAXB(我是技术负责人)来尝试您的示例。我们修复了与您的问题相关的错误,您需要使用今天(11月4日)或更高版本的EclipeseLink 2.2.0夜间下载:
@XmlRootElement
class Zoo {

  @XmlElementWrapper
  @XmlAnyElement(lax = true)
  private List<Animal> animals;

  public static void main(String[] args) throws Exception {
    Zoo zoo = new Zoo();
    zoo.animals = new ArrayList<Animal>();
    zoo.animals.add(new Dog());
    zoo.animals.add(new Cat());

    JAXBContext jc = JAXBContext.newInstance(Zoo.class, Dog.class, Cat.class);
    Marshaller marshaller = jc.createMarshaller();

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    marshaller.marshal(zoo, os);

    System.out.println(os.toString());

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Zoo unmarshalledZoo = (Zoo) unmarshaller.unmarshal(new ByteArrayInputStream(os.toByteArray()));

    if (unmarshalledZoo.animals == null) {
      System.out.println("animals was null");
    } else if (unmarshalledZoo.animals.size() == 2) {
      System.out.println("it worked");
    } else {
      System.out.println("failed!");
    }
  }

  public interface Animal {}

  @XmlRootElement
  public static class Dog implements Animal {}

  @XmlRootElement
  public static class Cat implements Animal {}
} 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<zoo><animals><dog/><cat/></animals></zoo>
it worked