目前,我正在捕捉异常并设置有效负载
<catch-exception-strategy name="Catch_Exception_Strategy">
<logger message="#[exception.summaryMessage]" level="ERROR" doc:name="Logger"/>
<set-payload value="Error in service operation" doc:name="Set Payload"/>
</catch-exception-strategy>
但是,CXF SOAP组件设置真实响应对象的有效负载。虽然我想在SOAP Fault中发送错误对象。我怎么能这样做?
提前致谢。
默认异常策略发送带有错误消息的SOAP Fault。最初我的目的是添加记录器并让默认错误消息进入SOAP Fault。我添加了Catch异常策略,但后来发现它没有发送SOAP错误。相反,我使用了发送SOAP错误的Rollback策略。但是,使用Mule Logger在策略块中记录异常并没有多大好处,因为它已经由Mule记录。
现在,我意识到使用cxf:outFaultInterceptor的优势。您不仅可以向SOAP Fault添加自定义元素,还可以在单独的日志文件中隔离自定义日志。感谢Anirban提供样本拦截器类。
答案 0 :(得分:2)
有很多方法可以做到这一点.. 其中一种方法是在Catch块中使用表达式组件,如下所示: -
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-variable variableName="id" value="Invalid Request !!!" doc:name="Variable"/>
<expression-transformer expression="#[message.payload="<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>\n
<soap:Body>\n <soap:Fault>\n <faultcode>INVALID_REQUEST</faultcode>\n <faultstring>Invalid Request</faultstring>\n <detail>\n <ns4:Fault xmlns='http://services.test.com/schema/report/SendReport' xmlns:ns4='http://services.test.com/schema/common/Fault'>\n <ns4:errCode>-2</ns4:errCode>\n <ns4:errorDesc>System Fault</ns4:errorDesc>\n <ns4:stackTrace>Invalid Request.Please ensure that you use the correct request header as per the schema</ns4:stackTrace>\n </ns4:Fault>\n </detail>\n </soap:Fault>\n </soap:Body>\n </soap:Envelope>"]" doc:name="Expression_Custom_Fault_Response"/>
</catch-exception-strategy>
其他方法是在CXF故障拦截器中使用Custom Java类: -
<cxf:jaxws-service service="MainData" serviceClass="Your Service class" doc:name="SOAPWithHeader" >
<cxf:outFaultInterceptors>
<spring:bean id="outfault" class="Your custom fault interceptor Java Class"/>
</cxf:outFaultInterceptors>
</cxf:jaxws-service>
更新的答案
自定义故障拦截器类: -
public class CustomSoapFault extends AbstractSoapInterceptor
{
private static final Log logger = LogFactory.getLog(CustomSoapFaultOutInterceptor.class);
public CustomSoapFault()
{
super(Phase.MARSHAL);
getAfter().add(Soap11FaultOutInterceptor.class.getName());
}
@Override
public void handleMessage(SoapMessage message) throws Fault
{
Fault fault = (Fault) message.getContent(Exception.class);
logger.error(fault.getMessage(), fault);
//custom message in response
Element detail = fault.getOrCreateDetail();
Element errorDetail = detail.getOwnerDocument().createElement("error-reply");
Element error = errorDetail.getOwnerDocument().createElement("error");
Element extRefNumber = errorDetail.getOwnerDocument().createElement("ExternalReferenceNumber");
Element partnerId = errorDetail.getOwnerDocument().createElement("PartnerID");
partnerId.setTextContent("123");
extRefNumber.setTextContent("321");
error.setTextContent("Contact the administrator");
errorDetail.appendChild(error);
errorDetail.appendChild(extRefNumber);
errorDetail.appendChild(partnerId);
detail.appendChild(errorDetail);
}
}
//在CXF拦截器中提供类
<cxf:jaxws-service>
<cxf:outFaultInterceptors>
<spring:bean class="com.mypackage.mule.interceptor.CustomSoapFault"/>
</cxf:outFaultInterceptors>
</cxf:jaxws-service>