繁体   English   中英

java.lang.Instantiation exception when unmarshalling XML file with Groovy, but in Java works

[英]java.lang.Instantiation exception when unmarshalling XML file with Groovy, but in Java works

我有一个 XSD 模式,它定义了一个复杂的类型层次结构:

<xsd:complexType abstract="true" name="Node">
    <xsd:complexContent>
        <xsd:extension base="core:DescribableElement">
            ...
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>
<xsd:complexType abstract="true" name="SuccessorNode">
    <xsd:complexContent>
        <xsd:extension base="test:Node">
            ...
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="StartNode">
    <xsd:complexContent>
        <xsd:extension base="test:SuccessorNode">
            ...
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>
<!-- other types omitted for brevity... -->

从这个 XSD 模式中,我使用 Gradle jaxb 插件生成了相应的 Java 类。

我的问题是,当我尝试解组根据此模式编写的 XML 文件时,在 Groovy 中出现错误,但在 Java 中它工作正常。

Java 代码:

File xmlFile = new File("test.xml");
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<Pipeline> element = (JAXBElement<Pipeline>) unmarshaller.unmarshal(xmlFile);

Groovy 代码:

def xmlFile = new File("test.xml")
def context = JAXBContext.newInstance(ObjectFactory.class)
def unmarshaller = context.createUnmarshaller()
def element = unmarshaller.unmarshal(xmlFile) as JAXBElement<Pipeline>

使用 Groovy 代码,我得到一个java.lang.InstantiationException

Caught: jakarta.xml.bind.UnmarshalException: Unable to create an instance of test.Node
 - with linked exception:
[java.lang.InstantiationException]
jakarta.xml.bind.UnmarshalException: Unable to create an instance of test.Node
 - with linked exception:
[java.lang.InstantiationException]
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:701)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportError(Loader.java:230)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:665)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:156)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.XsiTypeLoader.startElement(XsiTypeLoader.java:49)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:534)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:513)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:218)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:189)
    at jakarta.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:140)
    at jakarta.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:179)
    at jakarta.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:164)
    at Test.main(Test.groovy:13)
    at Test.run(Test.groovy:18)
Caused by: java.lang.InstantiationException
    at org.glassfish.jaxb.core.v2.ClassFactory.create0(ClassFactory.java:102)
    at org.glassfish.jaxb.runtime.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:252)
    at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:659)
    ... 12 more

似乎 Groovy 代码无法处理xsi:type属性内的名称空间,而 Java 代码可以很好地处理它们:

  • Java 代码正确地实例化了具体类;
  • 另一方面, Groovy 代码尝试实例化抽象代码(这会引发异常,因为无法实例化抽象 class )

为什么会这样? 有没有办法解决这种行为?

试试这个(假设 package 名称是“test”):

JAXBContext jc = JAXBContext.newInstance("test", test.ObjectFactory.class.getClassLoader());

暂无
暂无

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

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