要從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
能給我一些建議?謝謝! – user809965
您是在尋找作爲字符串還是作爲對象的SOAP內容? – ThomasRS