java 如何在 FaultMessageResolver 中解组 SOAP 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11108495/
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-10-31 03:50:45  来源:igfitidea点击:

How to unmarshal a SOAP Fault in a FaultMessageResolver?

javaweb-servicesjaxbspring-ws

提问by Eric B.

I'm new to Spring-WS, and slowly getting my head wrapped around it. Right now, I'm writing a simple client to communicate with an existing WS. I'm using the WebServiceTemplate and the marshalSendAndReceive method. Everything works fine.

我是 Spring-WS 的新手,并且慢慢地将我的头包裹在它周围。现在,我正在编写一个简单的客户端来与现有的 WS 进行通信。我正在使用 WebServiceTemplate 和 marshalSendAndReceive 方法。一切正常。

However, when a SOAP fault occurs, the WST throws a SoapFaultClientException. I noticed that I can create my own FaultMessageResolver to check what is contained within the SOAP fault. However, when I try to unmarshal the WebServiceMessage in my FaultMessageResolver, I get the following error message:

但是,当发生 SOAP 错误时,WST 会抛出 SoapFaultClientException。我注意到我可以创建自己的 FaultMessageResolver 来检查 SOAP 错误中包含的内容。但是,当我尝试在我的 FaultMessageResolver 中解组 WebServiceMessage 时,我收到以下错误消息:

JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault"). Expected elements are ....

Obviously, my unmarshaller is not properly configured. Do I have to generate the JAXB fault model myself using xjc to then be able to unmarshal the error? I am somewhat amazed that this does not already exist.

显然,我的解组器没有正确配置。我是否必须使用 xjc 自己生成 JAXB 故障模型才能解组错误?我有点惊讶这还不存在。

Is there a better way to extra my custom error information from within my soap:fault response? My error looks something like the following and I am trying to extract/access the serviceErrors item.

有没有更好的方法可以从我的 soap:fault 响应中提取我的自定义错误信息?我的错误类似于以下内容,我正在尝试提取/访问 serviceErrors 项目。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Blaze Data Error</faultstring>
         <detail>
            <ns14:serviceErrors xmlns="http://www.nb.com/ClientServices/LendingSimulation/CalculateProfitabilityRequest" xmlns:ns13="http://www.nb.com/fw/errorMgmt" xmlns:ns14="http://www.nb.com/ClientServices/LendingSimulation/V1" >
               <ns13:faultstring>ServiceExecutionError</ns13:faultstring>
               <ns13:serviceError>
                  <ns13:errorCode>C10F1013</ns13:errorCode>
                  <ns13:errorType>B</ns13:errorType>
                  <ns13:errorMessage>Unable to retreive additional data</ns13:errorMessage>
                  <ns13:fieldName>Message error received from PHClient :  [An unexpected error code was received : system=PH context=[empty]]</ns13:fieldName>
                  <ns13:systemId>Calculator</ns13:systemId>
                  <ns13:time>2012-06-19 14:45:10.151-0400</ns13:time>
               </ns13:serviceError>
            </ns14:serviceErrors>
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

Thanks!

谢谢!

Eric

埃里克

采纳答案by EdO

Based on the error you're getting, it looks like you are trying to unmarshall XML source starting with the <soap:Fault>node. Without seeing your Java code, this means you are probably trying to unmarshall the source from message.getSoapBody().getFault()

根据您收到的错误,您似乎正在尝试从<soap:Fault>节点开始解组 XML 源。没有看到您的 Java 代码,这意味着您可能正在尝试从message.getSoapBody().getFault()

You actually need to go deeper to get to the <ns14:serviceErrors>node. You should be able to get to this by the following path... maybe not all at once :)

您实际上需要更深入地到达<ns14:serviceErrors>节点。您应该能够通过以下路径到达这里...也许不是一下子:)

message.getSoapBody().getFault().getFaultDetail().getDetailEntries().next()

回答by Ondrej Burkert

I'm using org.springframework.ws.client.core.WebServiceTemplate. I run into the same issue with Faultunmarshalling. I resolved this problem by setting WebServiceTemplateproperty checkConnectionForFaultto false. So in my case it was likely caused by the server not returning status 500 for the fault.

我正在使用org.springframework.ws.client.core.WebServiceTemplate. 我在Fault解组时遇到了同样的问题。我通过将WebServiceTemplate属性设置checkConnectionForFaultfalse. 所以在我的情况下,这很可能是由于服务器没有为故障返回状态 500 造成的。

Then spring-wsparsed the fault and passed it to the default FaultMessageResolverand I got a regular SoapFaultClientExceptionto catch.

然后spring-ws解析错误并将其传递给默认值FaultMessageResolver,我得到了一个普通SoapFaultClientException的捕获。

But that got me only the message of the fault so I had to also add a custom FaultMessageResolverand extract the message along the same lines:

但这让我只得到了错误消息,所以我还必须添加一个自定义FaultMessageResolver并沿着相同的行提取消息:

SoapFaultDetail faultDetail = ((SoapMessage) message).getSoapBody().getFault().getFaultDetail();
Iterator<SoapFaultDetailElement> detailEntries = faultDetail.getDetailEntries();
StringBuilder errorMessage = new StringBuilder();

while (detailEntries.hasNext()) {
    SoapFaultDetailElement detailElement = detailEntries.next();
    Node node = ((DOMResult) detailElement.getResult()).getNode();
    CustomServiceFault serviceFault = (CustomServiceFault) marshaller.unmarshal(new DOMSource(node));
    errorMessage.append(serviceFault.getErrorCode() + ": " + serviceFault.getErrorMessage());
}
throw new CustomException(errorMessage.toString());

The marshaller is the same as the one used for the WebserviceTemplate.

编组器与用于WebserviceTemplate.