Spring WS: log full SOAP request/response

Here is a quick’n’dirty snippet I wrote to log the full content of SOAP requests/responses. Pretty useful for debugging purposes.

You can set the interceptor when creating the WebServiceTemplate object.

Spring WS version : 2.2.3.RELEASE

// import org.springframework.ws.*;
// ArrayUtils (apache commons-lang)
// WebServiceTemplate wsTemplate

ClientInterceptor[] interceptors = wsTemplate.getInterceptors();
        
interceptors = (ClientInterceptor[]) ArrayUtils.add(interceptors, new ClientInterceptor() {
    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
        return true;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
        return true;
    }

    @Override
    public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
        return true;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Exception ex) throws WebServiceClientException {
        try {
            System.out.println("Request :");
            messageContext.getRequest().writeTo(System.out);
            System.out.println("\nResponse : ");
            messageContext.getResponse().writeTo(System.out);
            System.out.println();
        } catch (IOException ignored) {
        }
    }
});

wsTemplate.setInterceptors(interceptors);

One thought on “Spring WS: log full SOAP request/response

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s