How to set a faultCode in a SOAPFault?
Asked Answered
M

2

8

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);
Misdoubt answered 23/9, 2012 at 19:39 Comment(0)
I
6

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>
Illsuited answered 8/3, 2017 at 16:36 Comment(0)
S
4

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.

Skirl answered 24/9, 2012 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.