Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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
Java JAXB和enum实现接口_Java_Jaxb2 - Fatal编程技术网

Java JAXB和enum实现接口

Java JAXB和enum实现接口,java,jaxb2,Java,Jaxb2,我不熟悉JAXB(也不熟悉Stackoverflow!),在尝试封送和解封包含实现接口的枚举列表的对象时遇到了问题。 我看了一眼,尝试了一个类似的例子: public class testXML { private static interface Animal { public String getName(); } @XmlRootElement private static class Lion implements Animal {

我不熟悉JAXB(也不熟悉Stackoverflow!),在尝试封送和解封包含实现接口的枚举列表的对象时遇到了问题。 我看了一眼,尝试了一个类似的例子:

public class testXML {
    private static interface Animal {
        public String getName();
    }

    @XmlRootElement
    private static class Lion implements Animal {
        @XmlElement
        private String name;
        public Lion() { name = "Lion"; }
        @Override public String getName() {
            return name;
        }
    }

    @XmlRootElement
    private static class Dog implements Animal {
        @XmlElement
        private String name;
        public Dog() { name = "Dog"; }
        @Override public String getName() {
            return name;
        }
    }

    @XmlRootElement
    private static class Zoo {
      @XmlElementRefs({
          @XmlElementRef(name = "lion", type = Lion.class, required = true),
          @XmlElementRef(name = "dog", type = Dog.class, required = true)
      })
      public List<Animal> animals;
      public Zoo(Animal ... animals) {
          this.animals = Arrays.asList(animals);
      }
      public Zoo() {
          animals = null;
      }
      public Animal getAnimal(int i) {
          return animals.get(i);
      }
    }

    public static void main(String[] args) throws JAXBException {
        File file = new File("testXML.xml");
        JAXBContext context = JAXBContext.newInstance(
                Zoo.class,
                Lion.class,
                Dog.class);
        //SerializableParameter<ChannelParamEnum> result = new SerializableParameter(ChannelParamEnum.PARAM_CH_FGAIN, 1.0);
        Zoo result = new Zoo(new Lion(), new Dog());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(result, file);

        Zoo unmar = (Zoo)context.createUnmarshaller().unmarshal(file);
        Animal anim = unmar.getAnimal(0);
        System.out.println(anim);
    }
}
显然这不是我的用例,我必须使用枚举而不是类。我环顾了很多地方,但没有找到答案,所以非常感谢您的帮助


谢谢大家!

我终于找到了解决办法。这足以改变

  @XmlElementRefs({
      @XmlElementRef(name = "lion", type = Lion.class),
      @XmlElementRef(name = "dog", type = Dog.class)
  })

无论如何,我仍然觉得我在JAXB利用率方面遗漏了很多


我希望这有帮助

我面临着完全相同的问题。除了公认的答案外,您是否还尝试过其他解决方案?
Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Invalid @XmlElementRef : Type "package.testXML$Lion" or any of its subclasses are not known to this context.
  @XmlElementRefs({
      @XmlElementRef(name = "lion", type = Lion.class),
      @XmlElementRef(name = "dog", type = Dog.class)
  })
  @XmlElements({
      @XmlElement(name="lion", type=Lion.class),
      @XmlElement(name="dog", type=Dog.class)
  })