简体   繁体   English

处理自定义故障异常时如何获取SOAP fault的faultcode

[英]How to get SOAP fault's faultcode when handling custom fault exception

Our system consumes SOAP Web Service, using JAX-WS client stubs generated based on service's WSDL. 我们的系统使用基于服务的WSDL生成的JAX-WS客户端存根来使用SOAP Web服务。 In case of error server returns SOAP faults like this: 如果出现错误,服务器返回SOAP错误,如下所示:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <s:Fault>
      <faultcode>SomeErrorCode</faultcode>
      <faultstring xml:lang="en-US">Some error message</faultstring>
      <detail>
        <ApiFault xmlns="http://somenamespace.com/v1.0" xmlns:a="http://somenamespace.com/v1.0" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <a:RequestId>123456789</a:RequestId>
          <a:CanRetry>true</a:CanRetry>
        </ApiFault>
      </detail>
    </s:Fault>
  </s:Body>

Based on WSDL SomeCustomFault exception class is generated and all service methods are declared to throw this (see below) exception. 基于WSDL生成SomeCustomFault异常类,并声明所有服务方法抛出此异常(见下文)异常。

@WebFault(name = "ApiFault", targetNamespace = "http://services.altasoft.ge/orders/v1.0")
public class SomeCustomFault
    extends Exception
{
    private ApiFault faultInfo;

    public SomeCustomFault(String message, ApiFault faultInfo) {
        super(message);
        this.faultInfo = faultInfo;
    }

    public SomeCustomFault(String message, ApiFault faultInfo, Throwable cause) {
        super(message, cause);
        this.faultInfo = faultInfo;
    }

    public ApiFault getFaultInfo() {
        return faultInfo;
    }
}

As you can see this custom fault exception extends Exception and not SOAPFaultException . 如您所见,此自定义错误异常扩展了Exception而不是SOAPFaultException Hovewer I need to get SOAP fault's faultcode which could be retrieved only from SOAPFaultException using getFaultCode method. Hovewer我需要获取SOAP fault的faultcode,它只能使用getFaultCode方法从SOAPFaultException中检索。 Could you tell me how can I reach SOAPFaultException or SOAP fault's faultcode in place where I catch above mentioned custom fault exception? 您能否告诉我如何在上面提到的自定义故障异常时到达SOAPFaultException或SOAP fault的faultcode?

You could implement a JAX-WS handler and add it to your client web service reference. 您可以实现JAX-WS 处理程序并将其添加到客户端Web服务引用中。 This will be given opportunity to handle the request message and response message OR notified of a fault. 这将有机会处理请求消息和响应消息或通知故障。

Create a SOAPHandler<SOAPMessageContext> and your handleFault() method will be passed the SOAPMessageContext . 创建SOAPHandler<SOAPMessageContext> ,您的handleFault()方法将传递给SOAPMessageContext From that you can getMessage().getSOAPBody().getFault() to get the SOAPFault , which contains getFaultCode() and getDetail() . 不同于可以getMessage().getSOAPBody().getFault()得到SOAPFault ,其中包含getFaultCode()getDetail()

Assign your new fault handler to your web service ref. 将新的故障处理程序分配给Web服务引用。 One way is via @HandlerChain . 一种方法是通过@HandlerChain It will be invoked prior to your catch clause. 它将在catch子句之前调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM