java - JAXB 抛出 InstantiationException 试图编码 xsi :type based on abstract class

标签 java xml jaxb moxy xsitype

我在使用继承和 JAXB 解码时遇到问题。我已经阅读了很多示例(特别是在 http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html 上的大量引用博客和此处非常相似的 SO 问题:JAXB xsi:type subclass unmarshalling not working),但仍然遇到困难。

像许多其他问题一样,我正在尝试创建一个对象的 XML 表示,该对象的字段依赖于子类来获取信息。我在编译时不知 Prop 体的子类实现是什么,所以 XmlSeeAlso 之类的东西并不真正可用。

在我的测试用例中,我有一个 Root 类,它有一个抽象类 (A),它有一个具体的子类型 (B):

@XmlRootElement
@ToString
    public class Root {

    private A theClass;

    public A getTheClass() {
        return theClass;
    }

    public void setTheClass(A theClass) {
        this.theClass = theClass;
    }
}

@ToString
public abstract class A {

}

@XmlRootElement
@ToString
public class B extends A {
    private  String b = "from B-" + System.currentTimeMillis()
    public String getB() {
       return b;
    }

    public void setB(String b) {
         this.b = b;
    }
}

@ToString 是 Lombok 项目的注解。

我可以毫无问题地编码:

@Test
public void marshallTest() {

    try {
        Root r = new Root();
        B b = new B();

        r.setTheClass(b);
        Class<?>[] classArray = new Class<?> [] { Root.class, B.class };

        JAXBContext context = JAXBContext.newInstance(classArray);
        Marshaller marshaller = context.createMarshaller();

        try (FileWriter fw = new FileWriter(JAXB_TEST_XML)) {
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(r, fw);
        }

        try(StringWriter sw = new StringWriter() ) {
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(r, sw);
            log.debug("{}", sw.toString());
        }

    } catch (IOException | JAXBException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

生成以下 xml:

<root>
   <theClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b">
      <b>from B-1375211003868</b>
   </theClass>
</root>

当我尝试解码(使用 MOXY JAXB 实现)时,我得到:

This class does not define a public default constructor, or the constructor raised an exception.
Internal Exception: java.lang.InstantiationException
Descriptor: XMLDescriptor(xml.A --> [])

使用以下代码:

@Test
public void unmarshall() {

    try {
        JAXBContext context = JAXBContext.newInstance(new Class<?>[] {Root.class, A.class});
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder db = documentBuilderFactory.newDocumentBuilder();

        Root r = null;
        try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(JAXB_TEST_XML))) {
            Document doc = db.parse(bis);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            JAXBElement<?> result = unmarshaller.unmarshal(doc, Root.class);
            r = (Root) result.getValue();
        }

        assertTrue(r != null & r.getTheClass() != null && r.getTheClass() instanceof B);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

我试过使解码命名空间感知(与 JAXB xsi:type subclass unmarshalling not working 一样,它没有用。我试过使用 XmlElementRef,它也不起作用。我试过最新的 glassfish api 和从 maven central 下载的实现(2.2.8 )。我已经尝试过 MOXY eclipse 持久性 JAXB 实现。两者都没有用。我尝试过在不使用文档生成器的情况下解码,但效果不佳。我尝试将 Root.class 和 A.class 传递到 JAXB 上下文中,这也不起作用。

我感觉我对正在发生的事情存在根本性的误解。任何提示或想法将不胜感激。谢谢。

最佳答案

更新 2

您可以使用一个库来动态确定子类并将该结果传递给 MOXy 以构建 JAXBContext。以下是输入的增强请求,建议将 Jandex 用于此目的。


更新 1

在 EclipseLink 1.2.0(2009 年 10 月 23 日发布)之前存在一个问题,即使所有设置都正确,也可能会抛出该异常。


您只需让 JAXBContext 知道 B 类。一种方法是在 A 类上利用 @XmlSeeAlso 注释。

import javax.xml.bind.annotation.XmlSeeAlso;

@XmlSeeAlso({B.class})
public abstract class A {

您还可以在用于引导 JAXBContext 的类中包含 B:

JAXBContext jc = JAXBContext.newInstance(Root.class, B.class);

关于java - JAXB 抛出 InstantiationException 试图编码 xsi :type based on abstract class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17955581/

相关文章:

java - 播放采样声音javax.sound.sampled时的巨大延迟

java - JAXB 序列化 POJO 中的 JsonElement 字段

java - 为什么 JAXB 不能正确处理命名空间的子元素?

java - 使用命名空间解码 XML 响应

Java XML 解析和写入导致缺少行尾

java - 不同 OracleDB 连接之间的共享事务

java - Java 方法在抛出异常时返回给 JNI 调用者的是什么?

javascript - jQuery 排序导致 iOS Safari 卡住

xml - 发布 xml 模式的标准方法

python - 如何在 XSLT 中使用 python 函数和变量?