繁体   English   中英

JAXB 和枚举实现接口

[英]JAXB and enum implementing interface

我是 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);
    }
}

这没有问题。 然后我尝试将 Lion 和 Dog 类切换为枚举并尝试以下操作:

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

    @XmlRootElement
    private static enum Lion implements Animal {
        SIMBA("Simba"),
        MUFASA("Mufasa"),
        SCAR("Scar");

        private String name;
        Lion(String name) { this.name = name; }
        @Override public String getName() {
            return name;
        }
    }

    @XmlRootElement
    private static enum Dog implements Animal {
        PONGO("Pongo"),
        PEGGY("Peggy");

        private String name;
        Dog(String name) { this.name = name; }
        @Override public String getName() {
            return name;
        }
    }

    @XmlRootElement
    private static class Zoo {
      @XmlElementRefs({
          @XmlElementRef(name = "lion", type = Lion.class),
          @XmlElementRef(name = "dog", type = Dog.class)
      })
      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(
                Lion.class,
                Dog.class,
                Zoo.class);
        Zoo result = new Zoo(Lion.SIMBA, Lion.SCAR, Dog.PONGO);
        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);
    }
}

此代码在运行时在 JAXBContext.newInstance 中失败

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)
  })

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

我希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM