为什么可以设置faulString,但不能在SOAPFault中设置自定义故障代码?当我引发异常时,SoapFaultException中不会出现文本“代码X”。有人可以告诉我为什么?谢谢。

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

throw new SOAPFaultException(soapFault);

最佳答案

可以通过以下示例在soap响应中获取故障代码:

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) {
    //
}

我得到以下 SOAP 故障:
<?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>

10-08 01:50