如何在spring-ws中解析SoapFaultClientException

时间:2017-04-10 17:18:58

标签: java spring web-services spring-ws soapfault

我正在使用spring-ws-2.3.1,在为webservices创建客户端的时候我得到 SoapFaultClientException ,如下所示,

<SOAP-ENV:Body>
  <SOAP-ENV:Fault>
     <faultcode>SOAP-ENV:Server</faultcode>
     <faultstring>There was a problem with the server so the message could not proceed</faultstring>
     <faultactor>InvalidAPI</faultactor>
     <detail>
        <ns0:serviceException xmlns:ns0="http://www.books.com/interface/v1">
           <ns1:messageId xmlns:ns1="http://www.books.org/schema/common/v3_1">5411</ns1:messageId>
           <ns1:text xmlns:ns1="http://www.books.org/schema/common/v3_1">Locale is invalid.</ns1:text>
        </ns0:serviceException>
     </detail>
  </SOAP-ENV:Fault>

我正在尝试获取&#34; messageId&#34;和&#34;文字&#34; ServiceException,但我不能。请找到下面的代码,

catch (SoapFaultClientException ex) {
        SoapFaultDetail soapFaultDetail = ex.getSoapFault().getFaultDetail(); // <soapFaultDetail> node
        // if there is no fault soapFaultDetail ...
        if (soapFaultDetail == null) {
            throw ex;
        }
        SoapFaultDetailElement detailElementChild = soapFaultDetail.getDetailEntries().next();
        Source detailSource = detailElementChild.getSource();
        Object detail = webServiceTemplate.getUnmarshaller().unmarshal(detailSource);
        System.out.println("Detail"+detail.toString());//This object prints the jaxb element
    }

&#34;详细信息&#34; object返回JaxbElement.Is有任何优雅的方法来解析soap错误。

任何帮助都应该受到赞赏。

2 个答案:

答案 0 :(得分:4)

最后,我能够解析soap fault异常,

catch (SoapFaultClientException ex) {
    SoapFaultDetail soapFaultDetail = ex.getSoapFault().getFaultDetail(); // <soapFaultDetail> node
    // if there is no fault soapFaultDetail ...
    if (soapFaultDetail == null) {
        throw ex;
    }
    SoapFaultDetailElement detailElementChild = soapFaultDetail.getDetailEntries().next();
    Source detailSource = detailElementChild.getSource();
    Object detail = webServiceTemplate.getUnmarshaller().unmarshal(detailSource);
    JAXBElement<serviceException> source = (JAXBElement<serviceException>)detail;
    System.out.println("Text::"+source.getText()); //prints : Locale is invalid.
}

我没有找到任何其他优雅的方式,所以我希望这应该是解决方案。

答案 1 :(得分:1)

以这种方式进行,无需解组。

            String DETAIL_PATH = "//*[local-name()='Detail']";
            SoapFaultDetail soapFaultDetail = ((SoapFaultClientException)exception).getSoapFault().getFaultDetail();
            DOMSource sourceNode = (DOMSource) soapFaultDetail.getSource();
            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression exp = null;
            //Cibling detail field
            try {
                exp = xPath.compile(DETAIL_PATH);
                NodeList nl = (NodeList) exp.evaluate(sourceNode.getNode(), XPathConstants.NODESET);
                if (nl == null || nl.getLength() < 1) {
                    return;
                } else {
                    //Get value here
                    String detailValue = nl.item(0).getTextContent();
                }
            }
            catch (XPathExpressionException e) {
                LOG.debug("Can't get detail from Exception");
            }