2010-05-11 48 views

回答

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

} 
+0

此解決方案的唯一問題是,如果在解組響應時出現問題,handleResponse方法將永遠不會被調用。我還沒有找到解決此問題的解決方案。 – 2010-08-05 16:46:55

4

spring-ws文檔: http://static.springsource.org/spring-ws/sites/2.0/reference/html/common.html#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