【问题标题】:com.sun.istack.SAXException2 : Instance ... is substituting "java.lang.Object", but ... is bound to an anonymous typecom.sun.istack.SAXException2 : 实例 ... 正在替换“java.lang.Object”,但 ... 绑定到匿名类型
【发布时间】:2014-01-14 15:55:39
【问题描述】:

我正在将一个项目从 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>

有什么建议吗?

【问题讨论】:

    标签: java xml jaxb jaxb2


    【解决方案1】:

    问题原来是匿名复杂类型的复杂嵌套。

    通过如下方式将它们分开,问题就消失了。作为额外的奖励,我得到了更多可重用的代码。

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

    【讨论】:

      【解决方案2】:

      在尝试编组从 hibernate-mapping-4.0.xsd 生成的一些 jaxb 对象时,我遇到了同样的异常

      抛出的异常似乎涉及作为 HibernateMapping 根类的内部类生成的两个类 - “Id”和“CompositeID”。这两个元素都在 XSD 中定义为嵌套的 complexTypes,就像在 @mdarwin 的情况下一样。通过将 complexType 定义移出(使它们成为 xsd 中“schema”元素的根元素),问题得到解决,对象也被成功编组。

      很遗憾,我本来希望使用未修改的 XSD 来生成我的 jaxb 对象 - 但找不到解决问题的其他方法。

      【讨论】:

        【解决方案3】:

        我注意到的情况不依赖于 xsd-schema 中的&lt;xs:complexType&gt; 定义。

        实际的问题是我们有从 xml-schema 生成的类扩展的 Java 类。

        并且这些类使用@XmlType(name = "") 注释以使它们匿名(即生成的标记不包含xsi:type 属性并且生成的xml 文件对于初始模式仍然有效。

        我在java, xsd & marshalling: jre bug, my fault or xsd issues? 中找到了该解决方案的线索。

        由于我无法修改 xsd(它太复杂并且已经共享给我们 API 的客户端),因此解决方案是:

        • 制作一个 jaxb-binder,排除要生成到 Java 文件中的 complexType,并说 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)注解:
        
            package my.existing.class;
        
            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(...)
            public class TheClassYouWantToExclude {
        
                // ...
                @XmlAnyElement(lax = true)
                protected TheClassFromWhichYouWantToExtend theClassFromWhichYouWantToExtend;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-23
          • 1970-01-01
          • 2023-04-09
          相关资源
          最近更新 更多