1

I'm preparing some web services with Spring WS. Due to it being contract-first, I followed its guide and prepared an XSD.

So far, my Web Service request XML looks like this (this is SoapUI rendered request):

<soapenv:Envelope ...>
   <soapenv:Header/>
   <soapenv:Body>
      <sch:WorkOrderRequest>
         <sch:workOrder>
            <sch:catOds>?</sch:catOds>
            <sch:descr>?</sch:descr>
            <sch:idEqui>?</sch:idEqui>
            <...>
         </sch:workOrder>
      </sch:WorkOrderRequest>
   </soapenv:Body>
</soapenv:Envelope>

Java Endpoint code:

@org.springframework.ws.server.endpoint.annotation.Endpoint
public class WorkOrderEndpoint {

    private static final String NAMESPACE_URI = ...;

    @ResponsePayload
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "WorkOrderRequest")
    public WorkOrderResponse handleWorkOrderRequest(@RequestPayload WorkOrderRequest request) throws Exception {
        // ...
    }

}

Request top level object

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {

})
@XmlRootElement(name = "WorkOrderRequest", namespace = "...")
public class WorkOrderRequest {

    @XmlElement(namespace = "", required = true)
    protected WorkOrder workOrder;

    // ...

}

WorkOrder "envelope":

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

    @XmlElement(namespace = ...)
    protected String catOds;

    @XmlElement(namespace = ...)
    protected String descr;

    @XmlElement(namespace = ...)
    protected String idEqui;

    // ...

}

I'm starting to wonder whether that extra WorkOrder wrap is any useful. Before removing it I'd like to hear some suggestions: should I get rid of it and move all the properties inside the request object? Could removing it make the web service less future proof?

1 Answer 1

2

Having an explicit wrapper allows your code to state its intent, without further knowledge of the endpoint.

Let's compare two versions of the XML:

Wrapped Version:

<soapenv:Envelope ...>
   <soapenv:Header/>
   <soapenv:Body>
      <sch:WorkOrderRequest>
         <sch:workOrder>
            <...>
         </sch:workOrder>
      </sch:WorkOrderRequest>
   </soapenv:Body>
</soapenv:Envelope>

Unwrapped Version

<soapenv:Envelope ...>
   <soapenv:Header/>
   <soapenv:Body>
      <sch:workOrder>
         <...>
      </sch:workOrder>
   </soapenv:Body>
</soapenv:Envelope>

To me, the differences are meaningful to a human. The "Wrapped" version has an XML tag whose name tells me how the <sch:workOrder> tags will be used. The <sch:WorkOrderRequest> tells me this is a request to process some work orders.

In the "Unwrapped" version I just see a work order. The XML tags don't tell me as a human what the machine will do with this information. The endpoint URL can help clear that up (say a POST to /workorders/process), but if you are just looking at Java code, you aren't going to see the URL.

The "Wrapped" version also gives you some flexibility in processing multiple work orders in the future if you want:

<soapenv:Envelope ...>
   <soapenv:Header/>
   <soapenv:Body>
      <sch:WorkOrderRequest>
         <sch:workOrder>
            <...>
         </sch:workOrder>
         <sch:workOrder>
            <...>
         </sch:workOrder>
         <sch:workOrder>
            <...>
         </sch:workOrder>
      </sch:WorkOrderRequest>
   </soapenv:Body>
</soapenv:Envelope>

You can still do that in the "Unwrapped" version, but again it begs the question, "what will be done with these multiple work orders?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.