简体   繁体   中英

How do I get java webservices to use extended classes

I generated a set of classes with apache cxf based on a big wsdl we got from our client. It uses an anyType to let it contain a whole bunch of different types. This is the wsdl

<complexType name="PayloadBase">
    <complexContent>
        <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
        </restriction>
    </complexContent>
</complexType>

This translates in the following java code

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PayloadBase")
public class PayloadBase {

    // empty as you can see

}

It turns out that any class that desires to use a child of this class doesnt know how to work with it. Say, for example, a subclass of PayloadBase is added to a wrapping object that also contains a timestamp, the following XML is generated

<bericht timestamp="2012-02-14T16:03:34.331+01:00"><payload/></bericht>

This is the result of sending the following class through the webservice code

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BerichtDescription", propOrder = {
    "payload"
})
public class BerichtDescription {

    @XmlElement(required = true)
    protected PayloadBase payload;
    @XmlAttribute(name = "timestamp", required = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar timestamp;

Any instance of PayloadBase used will never show in the resulting xml. I have verified with the debugger that the PayloadBase instance is indeed filled with content. How do I get java webservices to work with this setup?

我花了一段时间才弄清楚,但最后的窍门是为每个子类在PayloadBase类中添加@XmlSeeAlso批注。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM