Com.sun.istack.SAXException2: instance ... replaces "java.lang.Object", but ... is bound to an anonymous type

I am updating the project to jaxb 2.2.7 from version 1.x.

I have an application that has been working for a while, but in some answers I see this:

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

This worked fine in jaxb 1.0. I do not know what's the problem.

Here is an excerpt from xsd (which I cannot change since clients use it):

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

Any suggestions?

+6
source share
3 answers

The problem turned out to be a complex embedding of anonymous complex types.

Separating them as shown below, the problem has disappeared. And as an added bonus, I got more code for reuse.

    <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>
+4
source

jaxb obejcts, hibernate-mapping-4.0.xsd

, , -, , HibernateMapping - "Id" "CompositeID". XSD complexTypes, @mdarwin. complexType (, "schema" xsd), .

, XSD jaxb, .

+1

, , <xs:complexType> xsd-.

, Java, , xml-.

@XmlType(name = "") @XmlType(name = "") (. @XmlType(name = "") xsi:type xml ).

java, xsd & marshalling: jre, xsd? ,

xsd ( API), :

  • Create a jaxb binder that excludes complexType to be generated in the Java file and tells JAXB to use the existing class instead. The binding file looks like this:

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

  • In the existing class, the class we need to extend is NOT a JAXB-annotated class, it is just an abstract class:


    package my.existing.class;

    public abstract class TheClassFromWhichYouWantToExtend {
    }
  • A field that references this class from the parent class is annotated @XmlAnyElement(lax = true):

    package my.existing.class;

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

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

All Articles