CXF outgoing Interceptor get soap response body that is always null?

后端 未结 3 1201
暖寄归人
暖寄归人 2021-01-01 02:09

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:



        
相关标签:
3条回答
  • 2021-01-01 02:33

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

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

    0 讨论(0)
  • 2021-01-01 02:41

    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

    0 讨论(0)
  • 2021-01-01 02:46

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

    0 讨论(0)
提交回复
热议问题