Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

xml

jaxb

jaxb2

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?

like image 450
mdarwin Avatar asked Jan 14 '14 15:01

mdarwin


2 Answers

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>
like image 175
mdarwin Avatar answered Sep 19 '22 22:09

mdarwin


I got the same exception when attempting to marshal some jaxb obejcts that I had generated from the hibernate-mapping-4.0.xsd

The exceptions that were being thrown seemed to involve two classes that were generated as inner classes of the class HibernateMapping root class - "Id" and "CompositeID". Both of these elements were defined in the XSD as nested complexTypes, just as in @mdarwin's case. By moving the complexType definitions out (so that they were root elements of the "schema" element in the xsd) then the problem was resolved and the objects were successfully marshaled.

A shame as I would have liked to have used the unmodified XSD to generate my jaxb object - but could not find another way around the problem.

like image 23
twdrake Avatar answered Sep 19 '22 22:09

twdrake