gpt4 book ai didi

soap - CXF传出拦截器得到的 SOAP 响应正文始终为空吗?

转载 作者:行者123 更新时间:2023-12-04 13:29:01 28 4
gpt4 key购买 nike

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

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

最佳答案

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

关于soap - CXF传出拦截器得到的 SOAP 响应正文始终为空吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6438178/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com