簡體   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