views:

27

answers:

0

Hi,

I am trying to use a Web Service that defines a type with some mandatory fields having a default value:

<complexType name="DocumentRegistrationRequest">
    <sequence>
        <element name="title" type="string">
            <annotation>
                <documentation>Title/subject of the document</documentation>
            </annotation>
        </element>
        […]
        <element name="mailType" default="INTERNAL" type="tns:MailType">
            <annotation>
                <documentation>Mail type</documentation>
            </annotation>
        </element>
        […]
    </sequence>
</complexType>

I then use Weblogic's clientgen ant task (with type="JAX-WS") to generate the client code, which provides the corresponding class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DocumentRegistrationRequest", propOrder = {
    "title",
    […]
    "mailType",
    […]
})
public class DocumentRegistrationRequest {

    @XmlElement(required = true)
    protected String title;
    […]
    @XmlElement(required = true, defaultValue = "INTERNAL")
    protected MailType mailType;
    […]
    // getters and setters
}

I then use a DocumentService to call some operation with a DocumentRegistrationRequest parameter. I get an instance of this service by building an instance of its javax.xml.ws.Service class (called DocumentWebService) and call that class's getDocumentService() or getPort() method. This service is generated from a WSDL containing:

<message name="registerDocument">
    <part name="parameters" element="types:registerDocument"/>
</message>
[…]
    <operation name="registerDocument">
        <input message="tns:registerDocument"/>
        <output message="tns:registerDocumentResponse"/>
    </operation>

And tns:registerDocument is described by

<element name="registerDocument">
    <complexType>
        <sequence>
            <element name="request" type="tns:DocumentRegistrationRequest"/>
        </sequence>
    </complexType>
</element>

I build my DocumentRegistrationRequest with new and set appropriate values for required attributes that do not have a defaultValue, leaving the other parameters as-is. However, when the request is sent to the server, I see using SoapUI that there is no element for mailType:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt;
   <S:Body>
      <registerDocument xmlns="…">
         <request>
            <title>…</title>
            […]
            <!-- NO mailType element -->
            <otherElem>…</otherElem>
         </request>
      </registerDocument>
   </S:Body>
</S:Envelope>

The request is thus not valid, and gives an error like:

line 13: Expected element 'mailType@…' instead of 'otherElem@…' here in element request@…

As far as I know, for the default value to be taken into account from the server side, the request should contain an element <mailType/> which would be interpreted as <mailType>INTERNAL</mailType>. How can I ensure that this element is present (and thus send a valid request), without setting the default value explicitly? This would be preferred for ease of maintenance (future versions of the WSDL which change the default or add new fields with default values…) and to avoid messing with many fields I do not care about.