在Mule中设置SOAP错误 [英] Setting SOAP fault in Mule

查看:184
本文介绍了在Mule中设置SOAP错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在捕捉异常并设置有效负载

Currently this is how I'm catching exception and setting the payload

<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故障中发送一个错误对象。如何做到这一点?

But, CXF SOAP component sets the payload of real response object. While I want to send an error object in a SOAP Fault instead. How can I do that?

提前感谢

默认异常策略发送带有错误消息的SOAP Fault。最初我的目的是添加记录器,让默认错误信息进入SOAP Fault。我添加了Catch异常策略,但后来发现它不发送SOAP错误。而是使用发送SOAP错误的Rollback策略。然而,使用Mule Logger的策略块中记录异常没有太多的好处,因为它已经被Mule记录了。

Default exception strategy sends SOAP Fault with error message. Initially my purpose was to just add the logger and let the default error message go in the SOAP Fault. I added Catch exception strategy but later found out it does not send SOAP fault. Instead then I used Rollback strategy that send SOAP fault. However, there was not much benefit of logging exception inside strategy block using Mule Logger as it is already logged by Mule.

现在,我意识到使用cxf:outFaultInterceptor的优势。您不仅可以向SOAP Fault添加自定义元素,还可以在单​​独的日志文件中隔离自定义日志。感谢Anirban提供示例拦截器类。

Now, I realize the advantage of using cxf:outFaultInterceptor. You can not only add custom elements to SOAP Fault but you can also segregate custom logs in separate log file. Thanks Anirban for providing sample interceptor class.

推荐答案

有很多方法可以做。
一种方式正在使用Catch块中的表达式组件,如下所示: -

There are many ways to do it.. One of the way is using a expression component in Catch block like the following :-

<catch-exception-strategy doc:name="Catch Exception Strategy">
  <set-variable variableName="id" value="Invalid Request !!!" doc:name="Variable"/>
  <expression-transformer expression="#[message.payload=&quot;&lt;soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'&gt;\n  
   &lt;soap:Body&gt;\n      &lt;soap:Fault&gt;\n      &lt;faultcode&gt;INVALID_REQUEST&lt;/faultcode&gt;\n      &lt;faultstring&gt;Invalid Request&lt;/faultstring&gt;\n      &lt;detail&gt;\n      &lt;ns4:Fault xmlns='http://services.test.com/schema/report/SendReport' xmlns:ns4='http://services.test.com/schema/common/Fault'&gt;\n      &lt;ns4:errCode&gt;-2&lt;/ns4:errCode&gt;\n      &lt;ns4:errorDesc&gt;System Fault&lt;/ns4:errorDesc&gt;\n      &lt;ns4:stackTrace&gt;Invalid Request.Please  ensure that you use the correct request header as per the schema&lt;/ns4:stackTrace&gt;\n      &lt;/ns4:Fault&gt;\n      &lt;/detail&gt;\n      &lt;/soap:Fault&gt;\n     &lt;/soap:Body&gt;\n    &lt;/soap:Envelope&gt;&quot;]" doc:name="Expression_Custom_Fault_Response"/>
  </catch-exception-strategy>

其他方式是在CXF故障拦截器中使用自定义Java类: -

Other way is to use Custom Java class in CXF fault interceptor :-

<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>

UPDATED ANSWER

自定义故障拦截器类: -

Customise the fault interceptor class :-

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中提供类拦截器

//Provide the class in CXF interceptor

<cxf:jaxws-service>
   <cxf:outFaultInterceptors>
      <spring:bean class="com.mypackage.mule.interceptor.CustomSoapFault"/>
   </cxf:outFaultInterceptors>
</cxf:jaxws-service>

这篇关于在Mule中设置SOAP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆