Spring WS WebServicesTemplate / Jaxb2Marshaller客户端查看原始xml?

时间:2010-05-11 17:10:41

标签: jaxb spring-ws

是否可以使用WebServicesTemplate和Jxb2Marshaller作为编组引擎来查看Spring WS客户端的请求和响应?

我只是想记录xml,而不是对原始xml执行任何操作。

2 个答案:

答案 0 :(得分:4)

请参阅spring-ws文档: http://static.springsource.org/spring-ws/sites/2.0/reference/html/common.html#logging

您可以通过标准的Commons Logging界面记录消息:

  

要记录所有服务器端消息,只需将org.springframework.ws.server.MessageTracing记录器设置为DEBUG或TRACE级别。在调试级别,仅记录有效负载根元素;在TRACE级别上,整个邮件内容。如果您只想记录已发送的消息,请使用org.springframework.ws.server.MessageTracing.sent记录器;或org.springframework.ws.server.MessageTracing.received记录收到的消息。

     

在客户端,存在类似的记录器:org.springframework.ws.client.MessageTracing.sentorg.springframework.ws.client.MessageTracing.received

答案 1 :(得分:1)

能够弄清楚 - 如果你将这样的ClientInterceptor添加到WebServicesTemplate拦截器:

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);
            }
        }
    }

}