Spring WS WebServicesTemplate/Jaxb2Marshaller client view raw xml?

陌路散爱 提交于 2019-12-23 21:00:33

问题


Is it possible to view the request and the response for a Spring WS client using WebServicesTemplate and Jxb2Marshaller as the marshaling engine?

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


回答1:


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.




回答2:


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

}


来源:https://stackoverflow.com/questions/2812839/spring-ws-webservicestemplate-jaxb2marshaller-client-view-raw-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!