问题描述
我正在从Mule进行SOAP调用.我正在使用default-exception-strategy捕获异常.发生异常时,如何发送自己的故障代码和故障字符串,而不是通用的肥皂故障消息?谢谢
I am making SOAP call from Mule. I am using default-exception-strategy for catching exception. When exception occurred, how do I send my own fault code and fault string rather than generic soap fault message? Thanks
这是m子xml,java代码,实际输出和预期输出的代码段
Here is the snippet of mule xml, java code, actual output and expected output
<flow name="addressSearchFlow1" doc:name="addressSearchFlow1">
<invoke object-ref="InvoiceManager" method="completeSearchObject"
methodArguments="#[message.payload]"></invoke>
<logger level="INFO" doc:name="Logger" />
<processor-chain doc:name="Search Processor Chain">
<cxf:jaxws-client operation="doSearch"
clientClass="com.invoice.web_2007_09.ProWeb" port="InvoicePortType"
wsdlLocation="proweb.wsdl" enableMuleSoapHeaders="true" doc:name="SOAP">
</cxf:jaxws-client>
<http:outbound-endpoint exchange-pattern="request-response"
method="POST" ref="InvoiceEndPoint" doc:name="HTTP" />
</processor-chain>
<default-exception-strategy>
<processor-chain>
<logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />
<custom-transformer class="com.managers.CustomExceptionHandler" />
<logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />
</processor-chain>
</default-exception-strategy>
</flow>
Java代码
package com.managers;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.message.ExceptionMessage;
import org.mule.transformer.AbstractMessageTransformer;
public class CustomExceptionHandler extends AbstractMessageTransformer {
public CustomExceptionHandler() {
setName("CustomExceptionHandler");
}
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
ExceptionMessage exceptionMessage = (ExceptionMessage)message.getPayload();
String outputMessage = "<soap:Fault xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> "
+ " <faultcode>soap:Server</faultcode> "
+ "<faultstring>"
+ "An unexpected error has occured. Please contact your service desk and quote this error \"" + exceptionMessage.getException() + "\""+ "</faultstring> " + "</soap:Fault>";
return outputMessage;
}
}
实际输出
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring> dt-invoice. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: PostMethod</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
预期产量
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring> An unexpected error has occured. Please contact your service desk and quote this error "org.mule.api.transport.DispatchException: dt-invoice. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: PostMethod"
</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
推荐答案
如下更新流程以开始使用捕获异常策略.
Update the flow as below to start using Catch Exception Strategy.
<flow name="addressSearchFlow1" doc:name="addressSearchFlow1">
<invoke object-ref="InvoiceManager" method="completeSearchObject"
methodArguments="#[message.payload]"></invoke>
<logger level="INFO" doc:name="Logger" />
<processor-chain doc:name="Search Processor Chain">
<cxf:jaxws-client operation="doSearch"
clientClass="com.invoice.web_2007_09.ProWeb" port="InvoicePortType"
wsdlLocation="proweb.wsdl" enableMuleSoapHeaders="true" doc:name="SOAP">
</cxf:jaxws-client>
<http:outbound-endpoint exchange-pattern="request-response"
method="POST" ref="InvoiceEndPoint" doc:name="HTTP" />
</processor-chain>
<catch-exception-strategy>
<logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />
<custom-transformer class="com.managers.CustomExceptionHandler" />
<logger message="Exception_logger" level="INFO" doc:name="Exception_logger" />
</catch-exception-strategy>
</flow>
在自定义转换器中,消息中也不存在作为有效负载的异常.在exceptionPayload中可用.因此,使用message.getExceptionPayload()可以将ExceptionPayload作为结果.从exceptionPayload对象可以获取异常消息和其他详细信息.
Also in the custom transformer, the exception is not present in message as payload. it is available in the exceptionPayload. So use message.getExceptionPayload() which gives you ExceptionPayload as the result. From the exceptionPayload object the exception message and other details can be obtained.
希望这会有所帮助.
这篇关于发生ule肥皂异常时,如何发送自己的故障代码和故障字符串,而不是一般的肥皂故障消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!