Java 处理自定义故障异常时如何获取SOAP故障的故障代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20097261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:08:18  来源:igfitidea点击:

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

javasoapjax-wssoapfaultjbossws

提问by Nikoloz

Our system consumes SOAP Web Service, using JAX-WS client stubs generated based on service's WSDL. In case of error server returns SOAP faults like this:

我们的系统使用基于服务的 WSDL 生成的 JAX-WS 客户端存根来使用 SOAP Web 服务。如果出现错误服务器会返回这样的 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 SomeCustomFaultexception class is generated and all service methods are declared to throw this (see below) exception.

基于 WSDLSomeCustomFault生成异常类并声明所有服务方法以抛出此(见下文)异常。

@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 Exceptionand not SOAPFaultException. Hovewer I need to get SOAP fault's faultcode which could be retrieved only from SOAPFaultExceptionusing getFaultCodemethod. Could you tell me how can I reach SOAPFaultExceptionor SOAP fault's faultcode in place where I catch above mentioned custom fault exception?

正如您所看到的,这个自定义错误异常是 extendsException而不是SOAPFaultException。但是,我需要获取 SOAP 故障的故障代码,该代码只能使用getFaultCode方法从SOAPFaultException 中检索。您能告诉我如何在捕获上述自定义故障异常的地方到达SOAPFaultException或 SOAP 故障的故障代码吗?

采纳答案by Scott Heaberlin

You could implement a JAX-WS handlerand add it to your client web service reference. This will be given opportunity to handle the request message and response message OR notified of a fault.

您可以实现 JAX-WS处理程序并将其添加到您的客户端 Web 服务引用中。这将有机会处理请求消息和响应消息或通知的故障。

Create a SOAPHandler<SOAPMessageContext>and your handleFault()method will be passed the SOAPMessageContext. From that you can getMessage().getSOAPBody().getFault()to get the SOAPFault, which contains getFaultCode()and getDetail().

创建一个SOAPHandler<SOAPMessageContext>,您的handleFault()方法将通过SOAPMessageContext. 从中你可以getMessage().getSOAPBody().getFault()得到SOAPFault, 其中包含getFaultCode()getDetail()

Assign your new fault handler to your web service ref. One way is via @HandlerChain. It will be invoked prior to your catchclause.

将您的新故障处理程序分配给您的 Web 服务引用。一种方式是通过@HandlerChain。它将在您的catch条款之前被调用。