JaxbContext如何处理Java继承?

时间:2019-03-29 06:05:02

标签: java inheritance jaxb

我正在尝试解组POJO生成xsds。
但是,我收到此错误

  

java.lang.ClassCastException:无法将动物投掷到Dog

这是我的代码。
Animal.java

public class Animal { 
  .... }

Dog.java

public class Dog Extends Animal {
  public void validate() //Additional method
  { 
   ....
  }}

JAXBContext

String input = "..."; //xml file
JAXBContext jc = JAXBContext.newInstance(Dog.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<Dog> element = (JAXBElement<Dog> unmarshaller
   .unmarshal(new ByteArrayInputStream(input.getBytes());
Dog dog = element.getValue();

0 个答案:

没有答案