Search This Blog

Thursday, 20 December 2012

SOAP Webservices using Spring -3

As of now we have configured the web service and have also made our contract (or WSDL ) visible to the client. But what happens when the client actually sends a request? How do we handle it?
Just like in spring-mvc we write controllers to process requests similarly in spring-ws we create endpoints.
Consider the endpoint to handle our client requests:
public class RandomOperationsEndPoint {

    private static final Logger logger = Logger.getLogger(RandomOperationsEndPoint.class);
    private static final String NAMESPACE_URI = "";
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetRandomRequest")
    public GetRandomResponse handleRequest(
         @RequestPayload final GetRandomRequest activateServiceRequest)
         throws Exception {
        String time = new DateFormatter().print(new Date(),Locale.ENGLISH);
        String name = activateServiceRequest.getName();"received request at " + time + " for name " + name);
        GetRandomResponse getRandomResponse = new GetRandomResponse();
        return getRandomResponse;
The first thing of note is the @Endpoint annotation. The annotation like the @Controller annotation makes the class eligible to be instantiated as a bean (if using component -scan). It also indicates that the class is capable of handling XML messages in spring-ws.
Next is the @PayloadRoot annotation. This is added on a method. It tells Spring which type of requests can be processed by this method. The handleRequest method is capable of handling requests where the part in the received message id of type GetRandomRequest. The namespace attribute is the namespace of the part.
This is an example of annotation based mapping of handlers or PayloadRootAnnotationMethodEndpointMapping.
The actual code inside the method does nothing but return 5. It does not even care for the request details.
The other two annotations - @RequestPayload and @ResponsePayload are used for unmarshaling and marshaling the request and response objects.
I used the WebService Explorer provided within eclipse to test my webservice.
Sample Request:
<soapenv:Envelope xmlns:soapenv=""
The response returned was:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="">
        <ns2:GetRandomResponse xmlns:ns2="">
The logs of the execution is as below:
DEBUG  - Received request 
[<soapenv:Envelope xmlns:soapenv="" 
 ...//the actual request in xml format (displayed above) 
DEBUG  - SOAPElement 
[com.sun.xml.internal.messaging.saaj.soap.ver1_1.Body1_1Impl] implements SAAJ 1.3
PayloadRootAnnotationMethodEndpointMapping  - Looking up endpoint for 
- Endpoint mapping [] 
maps request to endpoint [public
.RandomOperationsEndPoint.handleRequest( throws java.lang.Exception]
- Testing endpoint adapter []
- Testing if argument resolver [
MessageContextMethodArgumentResolver@fdf48d] supports [class]
XmlRootElementPayloadMethodProcessor  - Unmarshalled payload request to 
- Invoking [public
throws java.lang.Exception] with arguments []
- Returning cached instance of singleton bean 'randomOperationsEndPoint'

- received request at Sep 3, 2012 for name test
As can be seen:
  1. The request was received at the server as XML(SOAP).
  2. Based on the message part and its namespace the PayloadRootAnnotationMethodEndpointMapping class attempts to identify the Endpoint to handle this request.
  3. After successful identification of an endpoint it verifies that the message body can be resolved into the request parameter of the Endpoint method. It then proceeds to unmarshal the received request.
  4. The call is now delegated from the servlet to the Endpoint method. as can be seen our log has been printed.
Now to checks the logs generated after our method was executed:
- Method [public
GetRandomRequest) throws java.lang.Exception] 
returned []
XmlRootElementPayloadMethodProcessor  - Marshalling [
random_schema.GetRandomResponse@17fc1e7] to response payload
Sent response [<SOAP-ENV:Envelope xmlns:SOAP-ENV="">
//The actual response
Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@8ab721
- Successfully completed request
- Publishing event in WebApplicationContext for namespace 'spring-ws-servlet': 
ServletRequestHandledEvent: url=[/sampleWS/Service/ws-operations]; 
client=[]; method=[POST]; 
servlet=[spring-ws]; session=[null]; user=[null]; time=[844ms]; status=[OK]
The logs indicate
  1. On completion of endpoint the control returned to the servlet.
  2. The received method response was then marshaled using jaxb to an XML.
  3. The response was then written back to the stream
If we want to handle an exception in the code we simply need to create a fault and return it.


  1. This is really wonderfull blog post. I noticed this blog further more useful information. Thanks for sharing your useful views..... Web Services in Lucknow

  2. Can you share this project code??

    I can use it as practice

  3. Can you please share the source code?. it's some what unclear without source code.

  4. Hello, Thanks for this info, but it's not quite understanding what you're saying. Could you please share your sample code ASAP? Thanks !

  5. Hi, thanks for the tutorial,

    I don't understand, don't you need a spring-ws-servlet.xml ?