2011-06-22 70 views
5

我寫了一個Interceptor用於測試。但是我在攔截器中獲得Soap消息體始終爲空。傳出CXF的Interceptor獲取總是空的soap響應體?

我CXF是Apache的CXF-2.4.0

bean.xml是這樣的:

<cxf:bus> 
    <cxf:outInterceptors> 
     <ref bean="myOutSoapInterceptor"/> 
    </cxf:outInterceptors> 
</cxf:bus> 

攔截文件:

public class MySoapInterceptorImpl extends AbstractSoapInterceptor implements IMySoapInterceptor { 

public MySoapInterceptorImpl() 
{ 
    super(Phase.WRITE); 
    addAfter(SoapOutInterceptor.class.getName()); 
} 


public void handleMessage(SoapMessage msg) throws Fault { 
    // TODO Auto-generated method stub 
    String soapContent ; 
    SOAPMessage sm = msg.getContent(SOAPMessage.class); 

    /*sm is always null!*/ 
    } 
} 
+0

能給我一些建議?謝謝! – user809965

+0

您是在尋找作爲字符串還是作爲對象的SOAP內容? – ThomasRS

回答

2

的信息取決於你是相在這一刻。您可以在 Interceptor doku中找到階段列表。如果您嘗試獲取消息內容,則需要查找消息存在的格式。看看getContentFormats。有些對象不會給你消息。 CXF最適用於流。所以流對象可以被刷新。

與問候 基督教

9

要從soap消息中獲得響應xml,可以使用「CacheAndWriteOutputStream」和「CachedOutputStreamCallback」。在回調類中,您可以在關閉流之前獲取消息。再說了,我們出去LoggingInterceptor是「wsLoggingOutInterceptor」,可以在上下文文件中配置如下:

<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> 
<bean id="logOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> 
<bean id="wsLoggingOutInterceptor" class="org.jinouts.webservice.logging.WSLoggingOutInterceptor"></bean> 

    <cxf:bus> 
     <cxf:inInterceptors> 
      <ref bean="loggingInInterceptor"/>   
     </cxf:inInterceptors> 
     <cxf:outInterceptors> 
      <ref bean="logOutInterceptor"/> 
      <ref bean="wsLoggingOutInterceptor"/> 
     </cxf:outInterceptors> 
    </cxf:bus> 

需要注意的是,在這裏我們也有一些默認攔截這是可用的CXF罐子。 現在,在我們自己的攔截器,我們可以通過以下方式寫入日誌的輸出響應消息,或者您也可以編輯在這裏:

/** 
* @author asraf 
* [email protected] 
*/ 
public class WSLoggingOutInterceptor extends AbstractLoggingInterceptor 
{ 
    public WSLoggingOutInterceptor() 
    { 
     super(Phase.PRE_STREAM); 
    } 

    @Override 
    public void handleMessage (Message message) throws Fault 
    { 
     // TODO Auto-generated method stub 
     OutputStream os = message.getContent (OutputStream.class); 
     CacheAndWriteOutputStream cwos = new CacheAndWriteOutputStream (os); 
     message.setContent (OutputStream.class, cwos); 

     cwos.registerCallback (new LoggingOutCallBack ()); 
    } 

    @Override 
    protected Logger getLogger () 
    { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 
class LoggingOutCallBack implements CachedOutputStreamCallback 
{ 
    @Override 
    public void onClose (CachedOutputStream cos) 
    { 
     try 
     { 
      if (cos != null) 
      { 
       System.out.println ("Response XML in out Interceptor : " + IOUtils.toString (cos.getInputStream ())); 
      } 

     } 
     catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }  
    } 

    @Override 
    public void onFlush (CachedOutputStream arg0) 
    { 

    } 
} 

看一看這個網站了解更多詳情:http://cxf.apache.org/docs/interceptors.html