【问题标题】:JAXB: Unmarshaller tries to instantiate abstract class (ignoring xsi:type)JAXB:Unmarshaller 尝试实例化抽象类(忽略 xsi:type)
【发布时间】:2012-02-27 18:32:56
【问题描述】:

几天以来,我一直在为一个奇怪的问题苦苦挣扎。客户提供 Web 服务 和一个 WSDL。我通过使用 wsimport 和一些 jaxb 自定义来导入 WSDL。所有Java 类的生成没有任何错误。

当我使用 Java 类解组响应时,出现以下异常:

06.02.2012 09:18:50 com.sun.xml.bind.v2.ClassFactory create
INFO: failed to create a new instance of class net.bipro.v2_1_0_1_0.namespace.datentypen.STEGefahr
java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.sun.xml.bind.v2.ClassFactory.create0(ClassFactory.java:122)
    at com.sun.xml.bind.v2.ClassFactory.create(ClassFactory.java:131)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:609)
    at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl$TransducerImpl.parse(RuntimeClassInfoImpl.java:392)
    at com.sun.xml.bind.v2.runtime.unmarshaller.TextLoader.text(TextLoader.java:69)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.text(UnmarshallingContext.java:514)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.processText(SAXConnector.java:181)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:154)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2939)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:217)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:189)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)
    at de.company.bipro.tarifierung.TestApp.<init>(TestApp.java:39)
    at de.company.bipro.tarifierung.TestApp.main(TestApp.java:143)

我将响应对象缩减为最小的有效 xml:

<taa:getQuoteResponse 
    xmlns:taa="http://www.bipro.net/namespace/tarifierung" 
    xmlns:leben="http://www.bipro.net/namespace/leben" 
    xmlns:sachen="http://www.bipro.net/namespace/sachen" 
    xmlns:haftpflicht="http://www.bipro.net/namespace/haftpflicht" 
    xmlns:fahrzeug="http://www.bipro.net/namespace/fahrzeug" 
    xmlns:xyzkfz="http://www.xyz.de/kfz/namespace" 
    xmlns:xyz-komposit="http://www.xyz.de/komposit/namespace" 
    xmlns:sach="http://www.bipro.net/namespace/sach" 
    xmlns:xyzleben="http://www.xyz.de/leben/namespace" 
    xmlns:xyz="http://www.xyz.de/namespace" 
    xmlns:partner="http://www.bipro.net/namespace/partner" 
    xmlns:mime="http://www.w3.org/2005/05/xmlmime" 
    xmlns:komposit="http://www.bipro.net/namespace/komposit" 
    xmlns:kfz="http://www.bipro.net/namespace/kraftfahrt" 
    xmlns:allgemein="http://www.bipro.net/namespace/allgemein">
  <taa:Response>    
    <taa:Tarifierung>      
      <taa:Verkaufsprodukt>        
        <taa:Produkt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="komposit:CT_SUHVersicherung">          
          <taa:Elementarprodukt xsi:type="komposit:CT_Deckung">            
            <komposit:Gefahr xmlns:daten="http://www.bipro.net/namespace/datentypen" xsi:type="daten:STE_GefahrBasis">L</komposit:Gefahr>            
          </taa:Elementarprodukt>
        </taa:Produkt>
      </taa:Verkaufsprodukt>      
    </taa:Tarifierung>
  </taa:Response>
</taa:getQuoteResponse>

这里是数据类型定义:

<xsd:complexType abstract="true" name="STE_Gefahr">
    <xsd:simpleContent>
        <xsd:extension base="xsd:string"/>
    </xsd:simpleContent>
</xsd:complexType>

<xsd:complexType final="#all" name="STE_GefahrBasis">
    <xsd:simpleContent>
        <xsd:restriction base="daten:STE_Gefahr">
            <xsd:enumeration value="F"><xsd:annotation><xsd:documentation>Feuer</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="L"><xsd:annotation><xsd:documentation>Leitungswasser</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="S"><xsd:annotation><xsd:documentation>Sturm</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="H"><xsd:annotation><xsd:documentation>Hagel</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="E"><xsd:annotation><xsd:documentation>Einbruchdiebstahl</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="G"><xsd:annotation><xsd:documentation>Glas</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="U"><xsd:annotation><xsd:documentation>Ueberspannungsschaeden</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="N"><xsd:annotation><xsd:documentation>Nutzfeuer</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M"><xsd:annotation><xsd:documentation>Elementarschaeden (alle Gefahren)</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="B"><xsd:annotation><xsd:documentation>Beraubung</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M01"><xsd:annotation><xsd:documentation>Ueberschwemmung</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M02"><xsd:annotation><xsd:documentation>Erdbeben</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M03"><xsd:annotation><xsd:documentation>Schneedruck</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M04"><xsd:annotation><xsd:documentation>Erdrutsch</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M05"><xsd:annotation><xsd:documentation>Vulkanausbruch</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M06"><xsd:annotation><xsd:documentation>Lawine</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M07"><xsd:annotation><xsd:documentation>Rueckstau</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M08"><xsd:annotation><xsd:documentation>Erdsenkung</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M09"><xsd:annotation><xsd:documentation>Starkregen</xsd:documentation></xsd:annotation></xsd:enumeration>
            <xsd:enumeration value="M99"><xsd:annotation><xsd:documentation>sonstige Elementarschaeden</xsd:documentation></xsd:annotation></xsd:enumeration>
        </xsd:restriction>
    </xsd:simpleContent>
</xsd:complexType>

我不知道为什么 xsi:type 会被忽略。特别是因为在其他元素中, 它也使用子类它工作正常。

我还用当前的 JAXBRI 替换了 JDK 附带的默认 jaxb - 仍然 同样的错误。

有人知道出了什么问题吗?我想,我已经“被线蒙蔽了”:-/

问候 鼠迷

【问题讨论】:

    标签: xsd wsdl jaxb jax-ws xsitype


    【解决方案1】:

    您的问题与报告的here 相同。它仍然在 Java 1.6.0_27 中体现出来,它似乎对应于 jaxb-ri-2.2-147。但是,如果使用 Java 1.7.0_02 执行相同的代码,则一切正常。这表明您正在使用的 JAXB 版本存在问题,因此请尝试切换到新的 JAXB/JDK(在我的情况下,成功的测试是在带有 JDK 1.7.0_02 的 NetBeans 7.1 上运行的)。

    【讨论】:

    • 感谢您的回答。尽管我安装了 JDK 7u2,但仍然遇到同样的错误。但为了确定这一点,我会检查 CLASSPATH 和使用的档案。
    • 肯定它必须工作。我能够重现它,并通过切换到 1.7,让它消失。我能够从示例 XML 中读取“L”,因此解组成功。
    【解决方案2】:

    您比我处于更好的位置。由于应用程序服务器,我绑定到 Java 1.5。在这种一般情况下,仍然没有解决方案,请参阅 http://java.net/jira/browse/JAXB-890

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 2017-02-05
      • 2021-02-12
      • 1970-01-01
      • 2015-08-04
      相关资源
      最近更新 更多