java 如何在 SOAPFault 中设置故障代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12555641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a faultCode in a SOAPFault?
提问by David
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 中设置故障字符串,但不能设置自定义故障代码?当我抛出异常时,文本“代码 X”不会出现在 SoapFaultException 中。有人能告诉我为什么吗?谢谢。
SOAPFault soapFault = SOAPFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createFault();
soapFault.setFaultString("String Y")
soapFault.setFaultCode("Code X");
throw new SOAPFaultException(soapFault);
回答by O. Sanchez
It is possible to get the fault code in the soap response with the following example:
可以使用以下示例在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) {
//
}
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>
回答by Paulius Matulionis
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
- aString
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.
SOAP 1.1 规范中定义了提供有关故障信息的故障代码。此元素在 SOAP 1.1 中是必需的。由于要求故障代码是 QName,因此最好使用此方法的 setFaultCode(Name) 形式。
faultCode
-String
给出要设置的故障代码。它必须是"prefix:localName"
在命名空间声明中定义前缀的形式。
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 thismethod and all should be OK.
请注意,您设置的故障代码必须是这种格式:prefix:localName
. 您正在设置:Code X
,这就是您看不到它的原因。使用这个方法,一切都应该OK了。