2

I have the following in a schema defining the request/response objects used in my web service request:

<xs:complexType name="loss" abstract="true">
    <xs:sequence>
        <xs:element name="lossDate" type="xs:dateTime"/>
        <xs:element name="lossDescription" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="autoLoss">
    <xs:complexContent>
        <xs:extension base="loss">
            <xs:sequence>
                <!-- autoLoss specific fields... -->
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="propLoss">
    <xs:complexContent>
        <xs:extension base="loss">
            <xs:sequence>
                <xs:element name="damageDescription" type="xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

And their associated Java classes:

@XmlType
public abstract class Loss {
    private Date lossDate;
    private String lossDescription;
    //getter & setters w/ JAXB annotations
}

@XmlType
public class AutoLoss extends Loss {
    //AutoLoss-specific fields...
    //getter & setters w/ JAXB annotations
}

@XmlType
public class PropLoss extends Loss {
    private String damageDescription;
    //getter & setters w/ JAXB annotations
}

The XML request I'm sending:

<testRequest>
    <myLoss xsi:type="propLoss">
        <lossDate>2001-12-17T09:30:47Z</lossDate>
        <lossDescription>test</lossDescription>
        <damageDescription>damageDesc</damageDescription>
    </myLoss>
</testRequest>

The Spring bean definition of the Jaxb2Marshaller:

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <!-- list of classes... -->
    </property>
    <property name="schema" value="/WEB-INF/mySchema.xsd" />
    <property name="adapters">
        <list>
            <bean class="com.lmig.ContactAdapter" />
        </list>
    </property>
</bean>

However, I'm getting a fault in the response stating: Unable to create an instance of com.lmig.Loss. If I remove the abstract modifier on Loss, the unmarshalled response object is of the base class type (Loss). How can I use Spring-WS and JAXB to properly unmarshall subclasses of abstract parent classes?

holic87
  • 791
  • 2
  • 17
  • 29

1 Answers1

2

You have an @XmlJavaTypeAdapter on each class in your hierarchy. Below is a link to an answer where I demonstrate how to mix Inheritance and XmlAdapter:

For More Information on JAXB and Inheritance:

For More Information on JAXB and XmlAdapter

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks Blaise. However, I don't think my `XmlAdapter` is the cause of this. I get the same error when JAXB tries to unmarshall a subclass that doesn't have `@XmlJavaTypeAdapter` on it. I have updated my original question to reflect this, so hopefully I'm more clear. – holic87 Oct 11 '11 at 22:11
  • 1
    @holic87 - Is your `JAXBContext` aware of the subclasses? You can either pass them in as a parameter when you create the JAXBContext, or add an `@XmlSeeAlso` annotation to the `Loss` class that references all its sub classes. – bdoughan Oct 11 '11 at 23:28
  • Yes, I've included the list of classes in the `propertiesToBeBound` property of my `Jaxb2Marshaller` Spring bean, including all base and subclasses. Additionally, turns out I was incorrect in saying that a `null` object is unmarshalled if I remove the `abstract` modifier from the base class: turns out _just_ the base class is being unmarshalled. I've updated my original question to reflect this. – holic87 Oct 12 '11 at 00:09