[英]How to unmarshal a SOAP Fault in a FaultMessageResolver?
I'm new to Spring-WS, and slowly getting my head wrapped around it. 我是Spring-WS的新手,慢慢地把头缠在它上面。 Right now, I'm writing a simple client to communicate with an existing WS. 现在,我正在编写一个简单的客户端来与现有的WS进行通信。 I'm using the WebServiceTemplate and the marshalSendAndReceive method. 我正在使用WebServiceTemplate和marshalSendAndReceive方法。 Everything works fine. 一切正常。
However, when a SOAP fault occurs, the WST throws a SoapFaultClientException. 但是,当发生SOAP错误时,WST会抛出SoapFaultClientException。 I noticed that I can create my own FaultMessageResolver to check what is contained within the SOAP fault. 我注意到我可以创建自己的FaultMessageResolver来检查SOAP错误中包含的内容。 However, when I try to unmarshal the WebServiceMessage in my FaultMessageResolver, I get the following error message: 但是,当我尝试在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. 显然,我的unmarshaller没有正确配置。 Do I have to generate the JAXB fault model myself using xjc to then be able to unmarshal the error? 我是否必须使用xjc自己生成JAXB故障模型,然后才能解组错误? I am somewhat amazed that this does not already exist. 我有点惊讶,这还不存在。
Is there a better way to extra my custom error information from within my soap:fault response? 有没有更好的方法从我的soap:故障响应中添加我的自定义错误信息? My error looks something like the following and I am trying to extract/access the serviceErrors item. 我的错误看起来像以下,我试图提取/访问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 埃里克
Based on the error you're getting, it looks like you are trying to unmarshall XML source starting with the <soap:Fault>
node. 根据您获得的错误,您似乎正在尝试从<soap:Fault>
节点开始解组XML源。 Without seeing your Java code, this means you are probably trying to unmarshall the source from message.getSoapBody().getFault()
如果没有看到您的Java代码,这意味着您可能正在尝试从message.getSoapBody().getFault()
解组源代码message.getSoapBody().getFault()
You actually need to go deeper to get to the <ns14:serviceErrors>
node. 您实际上需要更深入地访问<ns14:serviceErrors>
节点。 You should be able to get to this by the following path... maybe not all at once :) 您应该能够通过以下路径达到这个目标......也许不是一次全部:)
message.getSoapBody().getFault().getFaultDetail().getDetailEntries().next()
I'm using org.springframework.ws.client.core.WebServiceTemplate
. 我正在使用org.springframework.ws.client.core.WebServiceTemplate
。 I run into the same issue with Fault
unmarshalling. 我遇到了与Fault
unmarshalling相同的问题。 I resolved this problem by setting WebServiceTemplate
property checkConnectionForFault
to false
. 我通过将WebServiceTemplate
属性checkConnectionForFault
设置为false
来解决此问题。 So in my case it was likely caused by the server not returning status 500 for the fault. 所以在我的情况下,这很可能是由于服务器没有为故障返回状态500。
Then spring-ws
parsed the fault and passed it to the default FaultMessageResolver
and I got a regular SoapFaultClientException
to catch. 然后spring-ws
解析了错误并将其传递给默认的FaultMessageResolver
并且我得到了一个常规的SoapFaultClientException
来捕获。
But that got me only the message of the fault so I had to also add a custom FaultMessageResolver
and 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
. marshaller与用于WebserviceTemplate
的编组器相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.