开发者

Spring WS WebServicesTemplate/Jaxb2Marshaller client view raw xml?

开发者 https://www.devze.com 2022-12-29 18:46 出处:网络
Is it possible to view the request and the response for a Spring WS client using WebServicesTemplate and Jxb2Marshall开发者_开发技巧er as the marshaling engine?

Is it possible to view the request and the response for a Spring WS client using WebServicesTemplate and Jxb2Marshall开发者_开发技巧er as the marshaling engine?

I simply wan to to log the xml, not perform any actions upon the raw xml.


See the spring-ws documentation: http://static.springsource.org/spring-ws/sites/2.0/reference/html/common.html#logging

You can log messages via the standard Commons Logging interface:

To log all server-side messages, simply set the org.springframework.ws.server.MessageTracing logger to level DEBUG or TRACE. On the debug level, only the payload root element is logged; on the TRACE level, the entire message content. If you only want to log sent messages, use the org.springframework.ws.server.MessageTracing.sent logger; or org.springframework.ws.server.MessageTracing.received to log received messages.

On the client-side, similar loggers exist: org.springframework.ws.client.MessageTracing.sent and org.springframework.ws.client.MessageTracing.received.


Was able to figure it out - if you add a ClientInterceptor like this to the WebServicesTemplate interceptors:

package com.wuntee.interceptor;

import java.io.ByteArrayOutputStream;

import org.apache.log4j.Logger;
import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;

public class LoggerInterceptor implements ClientInterceptor {
    private Logger logger = Logger.getLogger(this.getClass().getName());

    public boolean handleFault(MessageContext context) throws WebServiceClientException {
        return false;
    }

    public boolean handleRequest(MessageContext context) throws WebServiceClientException {
        logger.info("handleRequest");
        logRequestResponse(context);        
        return true;
    }

    public boolean handleResponse(MessageContext context) throws WebServiceClientException {
        logger.info("handleResponse");
        logRequestResponse(context);
        return true;
    }

    private void logRequestResponse(MessageContext context){
        try{
            logger.info("Request:");
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            context.getRequest().writeTo(out);
            byte[] charData = out.toByteArray();
            String str = new String(charData, "ISO-8859-1");
            logger.info(str);
        } catch(Exception e){
            logger.error("Could not log request: ", e);
        }

        if(context.hasResponse()){
            try{
                logger.info("Response:");
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                context.getResponse().writeTo(out);
                byte[] charData = out.toByteArray();
                String str = new String(charData, "ISO-8859-1");
                logger.info(str);
            } catch(Exception e){
                logger.error("Could not log response: ", e);
            }
        }
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消