Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
How to Parse SoapFaultClientException in spring-ws

I am using spring-ws-2.3.1, While creating client for webservices sometime i am getting SoapFaultClientException like below,

<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>

I am trying to get the "messageId" and "Text" of the ServiceException but i couldn't.Please find the code below,

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
    }

The "detail" object returns the JaxbElement.Is there any elegant way to parse the soap fault.

Any help should be appreciated.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Finally,I can able to parse the soap fault exception,

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.
}

I don't find any other elegant way so i hope this should be the solution.

about 4 years ago · Santiago Trujillo Report

0

Did it this way, without unmarshaller.

            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");
            }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!