打印SOAP消息的XML内容
问题描述:
我正在为我的webservices使用Apache CXF。我创建了一个AbstractSoapInterceptor
的实例。在我的public void handleMessage(SoapMessage message) throws Fault
方法中,我想将拦截消息的XML内容打印到控制台。我怎样才能做到这一点?打印SOAP消息的XML内容
答
检查this输出并搜索INBOUND INTERCEPTOR。在这里将其作为参考......
public class InterceptorMensajeSOAPIn extends AbstractSoapInterceptor {
private static Logger log =
Logger.getLogger(InterceptorMensajeSOAPIn.class);
private SAAJInInterceptor saajIn = new SAAJInInterceptor();
public InterceptorMensajeSOAPIn(){
super(Phase.PRE_PROTOCOL);
getAfter().add(SAAJInInterceptor.class.getName());
}
public void handleMessage(SoapMessage message) throws Fault {
SOAPMessage soapMessage = getSOAPMessage(message);
try {
soapMessage.writeTo(System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
private SOAPMessage getSOAPMessage(SoapMessage smsg){
SOAPMessage soapMessage = smsg.getContent(SOAPMessage.class);
if (soapMessage == null) {
saajIn.handleMessage(smsg);
soapMessage = smsg.getContent(SOAPMessage.class);
}
return soapMessage;
}
}
答
任何原因,你不能只使用随机附带的CXF LoggingInInterceptor
?你可以直接获取代码并以此作为基础,但在2.3版本中,LoggingInInterceptor
已得到增强,允许指定打印流等,以便它可以“正常工作”。
答
您也可以使用此功能:org.apache.cxf.feature.LoggingFeature
:
<jaxws:endpoint ...>
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature"/>
</jaxws:features>
</jaxws:endpoint>
将与CXF 3.x的这一解决方案的工作?我试了一下,getContent的调用总是返回null,但也许我做了一些其他的错误,或者api在3.0中改变了。 – bwfrieds 2016-02-12 17:52:26