com.sun.istack.SAXException2 : Instance ... is substituting "java.lang.Object", but ... is bound to an anonymous type

java, jaxb, jaxb2, xml

Solution

The problem turned out to be the elaborate nesting of anonymous complex types.

By separating them out as below, the problem went away. And as an added bonus, I got more re-usable code.

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

Problem

I'm upgrading a project to jaxb 2.2.7 from version 1.x. I've got the app working some of the time, but on some responses 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've no idea what the problem could be. Here's an extract from the xsd (which I can't change, since clients are using 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?

Original source

Related problems