【问题标题】:JAXB throwing InstantiationException trying to marshal xsi:type based on abstract classJAXB 抛出 InstantiationException 试图基于抽象类编组 xsi:type
【发布时间】:2013-07-30 19:20:03
【问题描述】:

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

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

在我的测试用例中,我有一个带有抽象类 (A) 的 Root 类,该类具有一个具体的子类型 (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 也不起作用。我尝试了从 maven Central 下载的最新 glassfish api 和实现(2.2.8 ). 我已经尝试过 MOXY eclipse 持久性 JAXB 实现。两者都没有工作。我尝试在不使用文档构建器的情况下进行解组,但效果不佳。我尝试将 Root.class 和 A.class 传递到 JAXB 上下文中,这也不起作用。

我觉得我对正在发生的事情有一个基本的误解。任何提示或想法将不胜感激。谢谢。

  • Chooks

【问题讨论】:

    标签: java xml jaxb moxy xsitype


    【解决方案1】:

    更新 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);
    

    【讨论】:

    • 我想避免在编译时放入具体的类引用(参见第二段)。我正在使用来自 maven Central 的 moxy 2.5.0(org.eclipse.persistence 的组 ID 和 org.eclipse.persistence.moxy 的工件 ID)。我认为 xsi:type 规范将允许 JAXBContext 查找与该类型匹配的类。如果不是这种情况,那么我可能需要重新考虑我在做什么,并使用 XML 以外的其他方法。但是为了记录 - 是的 - 明确地告诉 JAXBContext 有关具体类的事情会起作用。
    • @chooks - 我已经更新了我的答案。您可能会使用另一个库来确定类,并将结果传递给 MOXy 以创建 JAXBContext
    • 感谢您指向 jandex。我曾想过尝试做类似的事情,但认为我的解组和 xsi:type 可能做错了什么。感谢您抽出宝贵时间解决我的问题。
    • 我有同样的问题,但没有任何效果。我正在使用 jaxb-ri-2.1-2(这是 Maven 为我选择的)并且是模式驱动的。 xjc 已将抽象类声明为 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "BaseProgramGroupTypeType") @XmlSeeAlso({ ProgramGroupTypeType.class }) public abstract class BaseProgramGroupTypeType { }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2014-01-22
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多