我是 Spring-WS 的新手,正在慢慢了解它。现在,我正在编写一个简单的客户端来与现有的 WS 进行通信。我正在使用 WebServiceTemplate 和 marshalSendAndReceive 方法。一切正常。
但是,当发生 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 ....
显然,我的解码器配置不正确。我是否必须自己使用 xjc 生成 JAXB 故障模型才能解码错误?我有点惊讶,这还不存在。
是否有更好的方法从我的 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>
谢谢!
埃里克
最佳答案
我正在使用 org.springframework.ws.client.core.WebServiceTemplate
。我在 Fault
解码时遇到了同样的问题。我通过将 WebServiceTemplate
属性 checkConnectionForFault
设置为 false
解决了这个问题。所以在我的情况下,这可能是由于服务器没有为错误返回状态 500 造成的。
然后 spring-ws
解析故障并将其传递给默认的 FaultMessageResolver
我得到一个常规的 SoapFaultClientException
来捕获。
但这只给我带来了故障消息,所以我还必须添加自定义 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());
编码器与用于 WebserviceTemplate
的编码器相同。
关于java - 如何解码 FaultMessageResolver 中的 SOAP 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11108495/