我正在从Mule进行SOAP调用。我使用默认异常策略来捕获异常。 发生异常时,如何发送自己的故障代码和故障字符串而不是通用的soap故障消息?感谢
以下是mule xml,java代码,实际输出和预期输出的片段
<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>
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>
答案 0 :(得分:0)
您的流程中需要<catch-exception-strategy' and not '<default-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>
<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>
</catch-exception-strategy>
</flow>
catch-exception-strategy :定义一个catch异常策略来自定义Mule处理任何异常的方式。 Catch异常策略使用入站消息。
default-exception-strategy :默认情况下定义并隐式应用以处理Mule应用程序中引发的所有消息传递异常
更多信息位于mulesoft
答案 1 :(得分:0)
按如下所示更新流程,开始使用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对象中,可以获取异常消息和其他详细信息。
希望这有帮助。