目前,我们在 Jboss EAP 6.2 上使用 CXF 2.7.3 时遇到问题,并出现自定义 SoapFault 异常。
当我们发送自定义 SoapFault 时,不会显示子代码及其值:
这是我们想要从 cxf 得到的:
<?xml version="1.0" encoding="UTF-8"?>
<tns:Fault
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tns="http://www.w3.org/2003/05/soap-envelope">
<tns:Code>
<tns:Value>tns:Sender</tns:Value>
<tns:Subcode>
<tns:Value>value</tns:Value>
</tns:Subcode>
</tns:Code>
<tns:Reason>
<tns:Text xml:lang="fr">
****
</tns:Text>
</tns:Reason>
<tns:Detail>
**Custom fault***
</tns:Detail>
</tns:Fault>
这是我们迄今为止所拥有的:
<?xml version="1.0" encoding="UTF-8"?>
<tns:Fault
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tns="http://www.w3.org/2003/05/soap-envelope">
<tns:Code>
<tns:Value>tns:Sender</tns:Value>
</tns:Code>
<tns:Reason>
<tns:Text xml:lang="fr">
****
</tns:Text>
</tns:Reason>
<tns:Detail>
**Custom fault***
</tns:Detail>
</tns:Fault>
子代码完全丢失。
我们尝试使用 CXF 中的自定义拦截器(从 LoggingOutInterceptor 或 AbstractInterceptor 扩展),如下所示来拦截自定义错误:
public class SoapRequestInterceptor extends LoggingOutInterceptor {
private static Logger log = Logger.getLogger(SoapRequestInterceptor.class);
public SoapRequestInterceptor() {
super(Phase.MARSHAL);
}
public void handleMessage(SoapMessage message) throws Fault{
SoapMessage soapMessage = message.getContent(SoapMessage.class);
if (soapMessage != null) {
log.info( "request intercepted:" + soapMessage.toString());
}
}
}
当我们将拦截器添加到 CXF 总线或 jaxws 拦截器时,甚至不会调用拦截器(它是在应用程序开始时添加的,因为它是通过构造函数获取的)。 我们如何拦截自定义soap故障消息并在CXF中编辑它?
非常感谢!
正如所问,这是我们在 spring applicationContext.xml 中声明拦截器的方式:
<cxf:bus>
<cxf:outFaultInterceptors>
<ref bean="soapRequestInterceptor" />
</cxf:outFaultInterceptors>
</cxf:bus>
<bean id="soapRequestInterceptor" class="fr.test.SoapRequestInterceptor" />
<jaxws:server serviceClass="fr.test.PriseEnChargeB2ServiceSP"
address="" serviceBean="#service">
<jaxws:binding>
<soap:soapBinding version="1.2" mtomEnabled="true" />
</jaxws:binding>
</jaxws:server>
注意:拦截器已很好实例化,但在我们的 WS 抛出 SOAP 故障后未调用
我们的 WS 末尾抛出的异常是这个:
public class PecSoapFaultException extends SoapFault {
private static final long serialVersionUID = 1L;
public TypeErreur erreur;
public PecSoapFaultException(String message, TypeErreur structure) {
super(message, new QName(""));
this.erreur = structure;
}
public PecSoapFaultException(String message, TypeErreur structure, QName faultcode) {
super(message, faultcode);
this.erreur = structure;
}
public PecSoapFaultException(String message, TypeErreur structure, QName faultcode,
QName subcode) {
super(message, faultcode);
this.setSubCode(subcode);
this.erreur = structure;
}
public TypeErreur getFaultInfo() {
return erreur;
}
最佳答案
你的拦截器没有被调用的问题是你没有重写正确的方法。您的代码应该如下所示:
@Override
public void handleMessage(Message message) throws Fault {
SoapMessage soapMessage = message.getContent(SoapMessage.class);
if (soapMessage != null) {
log.info( "request intercepted:" + soapMessage.toString());
}
}
然后你的拦截器将被调用。但正如我在评论中所说:如果出现故障, SOAP 消息将为空。因此您不会在日志中得到任何输出。
关于java - CXF网络服务: interceptor not triggered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36864775/