问题
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 theorg.springframework.ws.server.MessageTracing.sent
logger; ororg.springframework.ws.server.MessageTracing.received
to log received messages.On the client-side, similar loggers exist:
org.springframework.ws.client.MessageTracing.sent
andorg.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