CXF传出拦截器得到的肥皂响应正文始终为空吗? [英] CXF outgoing Interceptor get soap response body that is always null?

查看:175
本文介绍了CXF传出拦截器得到的肥皂响应正文始终为空吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个拦截器进行测试。但是我在Interceptor中得到的Soap消息正文始终为空。

I write a Interceptor for test. But I get Soap message body in the Interceptor is always null.

我的Cxf是Apache-CXF-2.4.0

My Cxf is Apache-CXF-2.4.0

bean.xml是这样的:

bean.xml is like this:

<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!*/
    }
 }


推荐答案

要从肥皂消息中获取响应xml,可以使用 CacheAndWriteOutputStream和 CachedOutputStreamCallback。在回调类中,您可以在关闭流之前获取消息。说,我们的LoggingInterceptor输出是 wsLoggingOutInterceptor,可以在上下文文件中进行如下配置:

To get the response xml from the soap message, you can use the "CacheAndWriteOutputStream" and "CachedOutputStreamCallback". In the callback class you can get the message before closing the stream. Say, our out LoggingInterceptor is "wsLoggingOutInterceptor" which can be configured in the context file as follows :

<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 jars一起使用。
现在,在我们自己的拦截器中,我们可以通过以下方式记录输出响应消息,或者您也可以在此处进行编辑:

Note that, here we have also some default interceptor which is available with the CXF jars. Now in our own interceptor we can write in the following way to log the output response message or you can also edit here :

/**
 * @author asraf
 * asraf344@gmail.com
 */
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

Have a look at this site for more details : http://cxf.apache.org/docs/interceptors.html

这篇关于CXF传出拦截器得到的肥皂响应正文始终为空吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆