为什么可以设置faulString,但不能在SOAPFault中设置自定义故障码?当我抛出异常时,文本"Code X“不会出现在SoapFaultException中。有人能告诉我为什么吗?谢谢。
SOAPFault soapFault = SOAPFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createFault();
soapFault.setFaultString("String Y")
soapFault.setFaultCode("Code X");
throw new SOAPFaultException(soapFault);发布于 2017-03-09 00:36:18
可以通过以下示例获取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>发布于 2012-09-24 21:54:37
来自documentation
故障代码是在SOAP1.1规范中定义的,它提供了有关故障的信息。此元素在SOAP1.1中是必需的。因为故障代码必须是名称,所以最好使用此方法的setFaultCode( QName )形式。
faultCode -给出要设置的故障代码的String。它的格式必须是在命名空间声明中定义了前缀的"prefix:localName"。
请注意,您设置的故障代码必须是这样的格式:prefix:localName。您正在设置:Code X,这就是为什么您看不到它的原因。使用this方法,一切都会好的。
https://stackoverflow.com/questions/12555641
复制相似问题