使用Spring-WS和JAXB:无法在XML请求中解组子类

时间:2011-10-11 17:21:08

标签: web-services jaxb spring-ws unmarshalling jaxb2

我在定义Web服务请求中使用的请求/响应对象的模式中有以下内容:

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

及其相关的Java类:

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

我发送的XML请求:

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

Jaxb2Marshaller

的Spring bean定义
<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>

但是,我在回复中说错了:Unable to create an instance of com.lmig.Loss。如果我删除abstract上的Loss修饰符,则解组的响应对象属于基类类型(Loss)。如何使用Spring-WS和JAXB正确解组抽象父类的子类?

1 个答案:

答案 0 :(得分:2)

您的层次结构中的每个类都有@XmlJavaTypeAdapter。下面是一个答案的链接,我将演示如何混合继承和XmlAdapter

有关JAXB和继承的更多信息:

有关JAXB和XmlAdapter

的更多信息