Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a faultCode in a SOAPFault?

Why can I set a faulString, but can't I set a custom fault code in a SOAPFault? When I throw the exception, the text "Code X" does not appear in the SoapFaultException. Someone could tell me why? Thanks.

SOAPFault soapFault = SOAPFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createFault();
soapFault.setFaultString("String Y")
soapFault.setFaultCode("Code X");

throw new SOAPFaultException(soapFault);
like image 742
David Avatar asked Sep 23 '12 19:09

David


2 Answers

It is possible to get the fault code in the soap response with the following example:

String faultString = "String Y";
String faultCodeValue = "Code X";
QName faultCode = new QName("nameSpaceURI", faultCodeValue);
SOAPFault soapFault = null;
try {
    soapFault = SOAPFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createFault(faultString, faultCode);
    throw new javax.xml.ws.soap.SOAPFaultException(soapFault);
} catch (SOAPException e1) {
    //
}

I get the following soap fault back:

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope" xmlns="">
        <faultcode xmlns:ns0="nameSpaceURI">ns0:Code X</faultcode>
        <faultstring>String Y</faultstring>
        </S:Fault>
    </S:Body>
</S:Envelope>
like image 65
O. Sanchez Avatar answered Sep 22 '22 20:09

O. Sanchez


From documentation:

Fault codes, which given information about the fault, are defined in the SOAP 1.1 specification. This element is mandatory in SOAP 1.1. Because the fault code is required to be a QName it is preferable to use the setFaultCode(Name) form of this method.

faultCode - a String giving the fault code to be set. It must be of the form "prefix:localName" where the prefix has been defined in a namespace declaration.

Notice that the fault code your're setting has to be this format: prefix:localName. You're setting: Code X, that is why you do not see it. Use this method and all should be OK.

like image 37
Paulius Matulionis Avatar answered Sep 22 '22 20:09

Paulius Matulionis