David Rabinowitz
David Rabinowitz

Reputation: 30448

Forcing Spring web services to add the xsd namespace to the response

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/"> 
   <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">          
    <soapenv:Body> 
         ... 
    </soapenv:Body> 
</soapenv:Envelope> 

How can I do it?

Upvotes: 4

Views: 6232

Answers (1)

skaffman
skaffman

Reputation: 403581

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.

Upvotes: 5

Related Questions