CXF传出拦截器获得始终为空的soap响应体? [英] CXF outgoing Interceptor get soap response body that is always null?

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

问题描述

我写了一个拦截器进行测试.但是我在拦截器中得到 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!*/
    }
 }

推荐答案

要从soap消息中获取响应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 jar.现在在我们自己的拦截器中,我们可以用以下方式编写来记录输出响应消息,或者您也可以在这里

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传出拦截器获得始终为空的soap响应体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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