本文介绍了JAXB注释 - 映射接口和@XmlElementWrapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JAXB注解一个字段是其泛型类型是一个接口列表的麻烦。当我把它声明,如:

  @XmlAnyElement
私人列表<动物>动物;

每一件事情正常工作。但是,当我添加一个包装元素,如:

  @XmlElementWrapper
@XmlAnyElement
私人列表<动物>动物;

我发现了Java对象正确乘警,但是当我通过取消编组编组创建的文档,我的列表是空的。我已经发布了低于code来证明这个问题。

我做得不对,或者这是一个错误?我曾与2.1.12版本和2.2-EA具有相同的结果试了一下。

我通过与设在这里的注解映射接口的例子工作:
https://jaxb.dev.java.net/guide/Mapping_interfaces.html

  @XmlRootElement
类动物园{  @XmlElementWrapper
  @XmlAnyElement(LAX =真)
  私人列表<动物>动物;  公共静态无效的主要(字串[] args)抛出异常{
    动物园动物园=新动物园();
    zoo.animals =新的ArrayList<动物>();
    zoo.animals.add(新狗());
    zoo.animals.add(新猫());    JAXBContext而JC = JAXBContext.newInstance(Zoo.class,Dog.class,那部分人来说);
    编组编组= jc.createMarshaller();    ByteArrayOutputStream OS =新ByteArrayOutputStream();
    的Marshaller.marshal(动物园,OS);    的System.out.println(os.toString());    解组解组= jc.createUnmarshaller();
    动物园unmarshalledZoo =(动物园)unmarshaller.unmarshal(新ByteArrayInputStream进行(os.toByteArray()));    如果(unmarshalledZoo.animals == NULL){
      的System.out.println(动物为空);
    }否则如果(unmarshalledZoo.animals.size()== 2){
      的System.out.println(它的工作);
    }其他{
      的System.out.println(失败!);
    }
  }  公共接口动物{}  @XmlRootElement
  公共静态类犬的动物实现{}  @XmlRootElement
  公共静态类Cat实现动物{}
}


解决方案

这是一个错误是固定在JAXB 2.1.13。更新您的图书馆或使用JDK 1.7或更高版本,问题也就解决了。

I am having trouble with JAXB annotations for a field that is a list whose generified type is an interface. When I have it declared such as:

@XmlAnyElement
private List<Animal> animals;

Every thing works correctly. But when I add a wrapper element, such as:

@XmlElementWrapper
@XmlAnyElement
private List<Animal> animals;

I find that the Java object marshals correctly, but when I unmarshal the document created by marshaling, my list is empty. I have posted below the code to demonstrate this problem.

Am I doing something wrong, or is this a bug? I have tried it with version 2.1.12 and 2.2-ea with the same result.

I am working through the example for mapping interfaces with annotations located here:https://jaxb.dev.java.net/guide/Mapping_interfaces.html

@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 {}
}
解决方案

This is a bug that was fixed in JAXB 2.1.13. Update your libraries or use JDK 1.7 or later and the problem will be solved.

这篇关于JAXB注释 - 映射接口和@XmlElementWrapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 19:38