views:

219

answers:

1

I'm using the Spring WS version 1.5.8. My response looks like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt; 
   <SOAP-ENV:Header/> 
   <SOAP-ENV:Body> 
      ...
   </SOAP-ENV:Body> 
</SOAP-ENV:Envelope>

However, my client (whom I integrate with) requires that I will add more namespace declerations in order for the parsing to succeed:

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;          
    <soapenv:Body> 
         ... 
    </soapenv:Body> 
</soapenv:Envelope> 

How can I do it?

+2  A: 

You probably don't need me to tell you that any SOAP client that requires certain namespace declarations to be present, when those namespaces are not used in the document, is broken. So I won't mention that.

However, if you do want to mutate the response like that, you can use an EndpointInterceptor, specifically a SoapEndpointInterceptor. You can wire up endpoint interceptors as described here.

Your custom interceptor can implement the handleResponse method, casting messageContext.getResponse() to SoapMessage and adding whatever you need to that:

public class MyInterceptor implements SoapEndpointInterceptor {

    public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
        SoapMessage message = (SoapMessage) messageContext.getResponse();
        message.getEnvelope().addNamespaceDeclaration(....);
        return true;
    }

    // ... other methods here
}

Adding such low-level namespace declarations may have side-effects, though, so tread carefully.

skaffman
Thanks skaffman for the quick response
David Rabinowitz