Java - How to show Soap Fault

cadaov picture cadaov · Sep 10, 2014 · Viewed 14.2k times · Source

I have this kind of response when having a Soap Fault calling a Java Web Service

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Server</faultcode>
            <faultstring>Fault occurred while processing.</faultstring>
            <detail>
                <ns1:WaybillRegistrationFault xmlns:ns1="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
                    <errors xmlns:ns2="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
                        <code>80000</code>
                        <description>El número de CTG 20140904 ya existe</description>
                    </errors>
                    <errors xmlns:ns2="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
                        <code>1000</code>
                        <description>La carta de porte ya se encuentra registrada.</description>
                    </errors>
                </ns1:WaybillRegistrationFault>
            </detail>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

I did an Soap Handler with its handleFault method like this: public boolean handleFault(SOAPMessageContext context) {

    try {
        System.err.println("Handler handleFault");

        if (context.getMessage().getSOAPBody().hasFault()) {
            SOAPFault fa = context.getMessage().getSOAPBody().getFault();
            System.err.println(fa.getFaultString());
            System.err.println("FaultCode: " + fa.getFaultCode() + " - Detail: " + fa.getDetail());
        }
        return true;
    } catch (SOAPException ex) {
        System.err.println("SoapEx " + ex.getMessage());
        return true;
    }
}

But in my output all I have is :

Handler handleFault
Fault occurred while processing.
FaultCode: soap:Server - Detail: [detail: null]

How do I process the errors node?

Update:

with fa.getDetail().getFirstChild().getTextContent() I get the text within the xml. How do I get that as an object. It be a WaybillRegistrationFault I think.

Answer

Ravi kiran Nageli picture Ravi kiran Nageli · Jan 9, 2018

This is the way that I always handle soap fault errors:

StringWriter sw = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(
new DOMSource(soapFaultException.getFault()), new StreamResult(sw));
String xml = sw.toString();