簡體   English   中英

com.sun.istack.SAXException2:Instance ...替換為“java.lang.Object”,但是...綁定到匿名類型

[英]com.sun.istack.SAXException2 : Instance … is substituting “java.lang.Object”, but … is bound to an anonymous type

我正在從1.x版升級項目到jaxb 2.2.7。

我的應用程序有時會工作,但在一些回復中我看到了這個:

java.lang.RuntimeException: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: Instance of "com.mycompany.global.er.decoupling.binding.response.PricePointType$BalanceImpactRates$BalanceImpactRate" 
is substituting "java.lang.Object", but 
"com.mycompany.global.er.decoupling.binding.response.PricePointType$BalanceImpactRates$BalanceImpactRate"
 is bound to an anonymous type.]

這在jaxb 1.0中運行良好。 我不知道問題是什么。

這是xsd的摘錄(我無法更改,因為客戶端正在使用它):

<xs:complexType name="price-pointType">
    <xs:sequence>
        <xs:element name="id" type="xs:string" />
.........
        <xs:element name="duration" type="durationType" />
        <xs:element name="order" type="xs:int" />
        <xs:element name="min-sub-period" type="xs:int" />
        <xs:element name="balance-impacts" minOccurs="0">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="balance-impact" type="charging-resourceType"
                        minOccurs="0" maxOccurs="unbounded" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="balance-impact-rates" minOccurs="0">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="balance-impact-rate" minOccurs="0"
                        maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="rate" type="xs:double" />
                            </xs:sequence>
                            <xs:attribute name="charging-resource-code" type="xs:string"
                                use="required" />
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>

有什么建議?

問題結果是匿名復雜類型的精心嵌套。

通過如下將它們分開,問題就消失了。 作為額外的獎勵,我獲得了更多可重復使用的代碼。

    <xs:complexType name="balanceImpactRate">
    <xs:sequence>
        <xs:element name="rate" type="xs:double" />
    </xs:sequence>
    <xs:attribute name="charging-resource-code" type="xs:string"
    use="required" />

</xs:complexType>


<xs:complexType name="balanceImpactRates" >
    <xs:sequence>
        <xs:element name="balance-impact-rate" type="balanceImpactRate"   minOccurs="0"
            maxOccurs="unbounded">
        </xs:element>
    </xs:sequence>
</xs:complexType>

在嘗試編組我從hibernate-mapping-4.0.xsd生成的一些jaxb obejct時,我遇到了同樣的異常

拋出的異常似乎涉及兩個類,這些類是作為類HibernateMapping根類的內部類生成的 - “Id”和“CompositeID”。 這兩個元素都在XSD中定義為嵌套的complexTypes,就像@mdarwin的情況一樣。 通過移動complexType定義(使它們是xsd中“schema”元素的根元素),問題得到解決,對象被成功封送。

遺憾的是,我本來希望使用未經修改的XSD來生成我的jaxb對象 - 但無法找到解決問題的另一種方法。

我注意到的情況不依賴於xsd-schema中的<xs:complexType>定義。

實際問題是我們有從類xml-schema生成的類擴展的 Java類。

並且這些類使用@XmlType(name = "")進行注釋以使它們匿名(即生成的標記不包含xsi:type屬性,並且生成的xml文件對初始模式仍然有效。

我在java,xsd和marshalling中發現了這個解決方案的線索:jre bug,我的錯誤還是xsd問題?

由於我無法修改xsd(它太復雜並且已經與我們的API的客戶端共享),解決方案是:

  • 創建一個jaxb-binder,它將要生成的complexType排除在Java文件中,並說JAXB使用現有的類來代替。 綁定文件如下所示:

     <jxb:bindings schemaLocation="MySchema.xsd"> <jxb:bindings node="//xs:complexType[@name='TheClassYouWantToExclude']"> <jxb:class ref="my.existing.class.TheClassYouWantToExclude"/> </jxb:bindings> </jxb:bindings> 

  • 在現有的類中,我們需要擴展的類不是JAXB注釋類,只是一個抽象類:


    package my.existing.class;

    public abstract class TheClassFromWhichYouWantToExtend {
    }
  • 從父類鏈接到此類的字段使用@XmlAnyElement(lax = true)注釋@XmlAnyElement(lax = true)

    package my.existing.class;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(...)
    public class TheClassYouWantToExclude {

        // ...
        @XmlAnyElement(lax = true)
        protected TheClassFromWhichYouWantToExtend theClassFromWhichYouWantToExtend;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM