CXF 传出拦截器获取始终为空的肥皂响应正文?

发布于 2024-11-16 18:02:30 字数 735 浏览 8 评论 0原文

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

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

My Cxf is Apache-CXF-2.4.0

bean.xml is like this:

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

Interceptor file :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

姜生凉生 2024-11-23 18:02:30

要从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 jar 中使用的默认拦截器。
现在,在我们自己的拦截器中,我们可以按以下方式编写来记录输出响应消息,或者您也可以在此处进行编辑:

/**
 * @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

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>

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
 * [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 )
    {

    }   
}

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

束缚m 2024-11-23 18:02:30

该消息取决于您此时所处的阶段。您可以找到包含以下阶段的列表
拦截器 doku。如果您尝试获取消息内容,您需要找到消息存在的格式。看看 getContentFormats。有些对象不会给您该消息。 CXF 大多数时候都与流一起工作。所以流对象可以被刷新。

最诚挚的问候
基督教

The message depends on the phase you are at this moment. You can find a list with phases in
Interceptor doku. If you try to get the message content you need to find our in which format the message exists. Have a look in getContentFormats. Some of the objects will not give you the message. The most time CXF works with streams. So the stream object could be flushed.

With best regards
Christian

吾性傲以野 2024-11-23 18:02:30

您的拦截器必须在 SAAJOutInterceptor() 之后运行;
例如Glen Mazza 的博客

Your intercepter must be run after SAAJOutInterceptor();
For example Glen Mazza's Weblog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文