【问题标题】:Handle Soap Fault in Spring Integration在 Spring 集成中处理 Soap 故障
【发布时间】:2014-05-21 06:51:38
【问题描述】:

我有以下设置连接如下:

1) 响应和请求通道
2) 用于 ws 对系统值对象的响应/请求的转换器
3) WS 请求/响应通道
4) 出站网关

      <!-- ~~~~~~~~~~~~~~~~~ -->
  <!-- integration layer -->
  <!-- ~~~~~~~~~~~~~~~~~ -->
  <int:channel id="getStatusRequestChannel"/>
  <int:channel id="getStatusResponseChannel"/>
  <int:channel id="getStatusWSRequestChannel"/>
  <int:channel id="getStatusWSResponseChannel"/>


  <!-- ~~~~~~~~~~~~~~~~~~ -->
  <!-- gateway definition -->
  <!-- ~~~~~~~~~~~~~~~~~~ -->
  <int:gateway id="mnpGateway" service-interface="com.iquest.play.integration.mnp.MNPGateway">
    <int:method name="getMNPStatus" request-channel="getStatusRequestChannel" reply-channel="getStatusResponseChannel"/>
  </int:gateway>

  <!-- ~~~~~~~~~~~~~~ -->
  <!-- channel chains -->
  <!-- ~~~~~~~~~~~~~~ -->
  <int:chain input-channel="getStatusRequestChannel" output-channel="getStatusWSRequestChannel">
    <int:transformer ref="getStatusTransformer" method="transformMNPStatusRequest"/>
  </int:chain>
  <int:chain input-channel="getStatusWSResponseChannel" output-channel="getStatusResponseChannel">
    <int:transformer ref="getStatusTransformer" method="transformMNPStatusResponse"/>
  </int:chain>

  <!-- ~~~~~~~~~~~~~~~~ -->
  <!-- outbound gateway -->
  <!-- ~~~~~~~~~~~~~~~~ -->
  <int-ws:outbound-gateway id="getStatusOutboundGW"
                           request-channel="getStatusWSRequestChannel"
                           reply-channel="getStatusWSResponseChannel"
                           marshaller="marshaller"
                           unmarshaller="marshaller"
                           destination-provider="mnpUriProvider"/>


这是 WSDL:

 <wsdl:operation name="getCaseInfo">
  <wsdl:documentation>Message</wsdl:documentation>
  <wsdl:input message="tns:GetCaseInfoRequest">
  </wsdl:input>
  <wsdl:output message="tns:GetCaseInfoResponse">
  </wsdl:output>
  <wsdl:fault message="tns:GetCaseInfoError" name="getCaseInfoError">
  </wsdl:fault>
</wsdl:operation>

如何捕捉肥皂故障?

编辑后:

我尝试扩展 SoapFaultMessageResolver 并覆盖该方法
public void resolveFault(WebServiceMessage message) throws IOException
从那里我试图抛出一个自定义的 IntegrationException(扩展 IOException),我将在调用网关接口的方法中捕获它。这是调用方法:

    try {
      gateway.MethodA();
    } catch (Exception e) {
      /// I was trying to catch IntegrationException
    }

问题是捕获的异常类型为WebServiceIOException,其根本原因为IntegrationException,它会触发一个巨大的错误日志。所以我觉得这种做法是不对的。

【问题讨论】:

    标签: java spring web-services soap wsdl


    【解决方案1】:

    &lt;int-ws:outbound-gateway&gt; 完全基于 Spring WS 中的 WebServiceTemplate,因此 Spring Integration 中的 Soap Fault 没有任何停止。

    默认情况下,它将是一个WebServiceException,它从WebServiceTemplate.sendAndReceive 抛出并传播到MessageHandler,后者又将它抛出给调用者或作为Message 发送到error-channel payload.

    只要您使用&lt;int:gateway&gt;Exception 就可能会在方法调用中被捕获。

    如果您想在重新抛出之前对该故障执行一些逻辑,您可以将fault-message-resolver 作为FaultMessageResolver 的实现注入&lt;int-ws:outbound-gateway&gt;

    【讨论】:

    • 另一个相关问题,例如,如果我从服务器收到的作为响应的消息不是错误,而是自定义异常消息。我该如何处理这种情况?根据消息类型选择行为?
    • 没错!在&lt;int-ws:outbound-gateway&gt; 之后放置一个&lt;router&gt; 并决定在哪里进一步发送消息
    • 尽量不要让你的IntegrationException 成为IOException 只是RuntimeException
    • 如果我希望它成为检查异常,还有其他选择吗?
    • 它应该是除IOException之外的任何一个,TransportException也不是,因为WebServiceTemplate包装了它们。但是从这里你不能这样做,因为resolveFault 方法签名只允许IOException 被选中
    【解决方案2】:

    我们可以在解组期间处理soap错误消息并将适当的消息抛出给错误处理程序。

    这篇文章清楚地解释了如何处理soap错误消息。希望这可能会有所帮助

    http://blog.hostmasterzone.com/how-to-unmarshal-soap-fault-in-spring-integration-web-service/

     <bean id="hmzJaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
             <property name="contextPaths">
                    <list>
                        <value>org.hmz.request.types</value>
                    </list>
                </property>
            </bean>
    <bean id="custJaxbUnMarshaller" class="com.jay.hmz.util.CustJaxbUnMarshaller" >
         <property name="contextPaths">
                <list>
                    <value>org.hmz.request.types</value>
                </list>
            </property>
        </bean>
    
        <int:chain input-channel="channel.in" output-channel="channel.out"> 
             <int:transformer method="transformParentRequestById"><bean class="org.jay.hmz.api.transformers.OrderTransformer"/></int:transformer>
             <int-ws:header-enricher><int-ws:soap-action value="${order.request.uri}"/></int-ws:header-enricher> <int-ws:outbound-gateway interceptors="hmzSecurityInterceptor" mapped-request-headers="GUID, USER_REF" request-callback="hmzWebServiceMessageCallback" unmarshaller="custJaxbUnMarshaller" marshaller="hmzJaxbMarshaller" uri="${order.request.uri}"/> 
             <int:transformer method="transformRetrieveParentOrderResponse"><bean class="org.jay.hmz.api.transformers.OrderTransformer"/></int:transformer> 
         </int:chain>
    
        <int:service-activator input-channel="requestErrorChannel" output-channel="response.out" ref="requestErrorHandler" method="handleFailedOrderRequest"/>
    
        <bean id="requestErrorHandler" class="org.jay.hmz.api.errorhandler.RequestErrorHandler"/>
    
    public class CustJaxbUnMarshaller extends Jaxb2Marshaller {
    
        @Override
        public Object unmarshal(Source source, MimeContainer mimeContainer)
                throws XmlMappingException {
            LOGGER.debug("Inside Custom JaxbWrapper unmarshal");
            Object mimeMessage = new DirectFieldAccessor(mimeContainer)
                    .getPropertyValue("mimeMessage");
            Object unmarshalObject = null;
            if (mimeMessage instanceof SaajSoapMessage) {
                SaajSoapMessage soapMessage = (SaajSoapMessage) mimeMessage;
                String faultReason = soapMessage.getFaultReason();
                if (faultReason != null) {
                    throw convertJaxbException(new JAXBException(faultReason));
                } else {
                    unmarshalObject = super.unmarshal(source, mimeContainer);
                }
            }
            return unmarshalObject;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      相关资源
      最近更新 更多