Java - 如何显示 Soap Fault
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25771670/
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
Java - How to show Soap Fault
提问by cadaov
I have this kind of response when having a Soap Fault calling a Java Web Service
当 Soap Fault 调用 Java Web 服务时,我有这种响应
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Fault occurred while processing.</faultstring>
<detail>
<ns1:WaybillRegistrationFault xmlns:ns1="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
<errors xmlns:ns2="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
<code>80000</code>
<description>El número de CTG 20140904 ya existe</description>
</errors>
<errors xmlns:ns2="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
<code>1000</code>
<description>La carta de porte ya se encuentra registrada.</description>
</errors>
</ns1:WaybillRegistrationFault>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
I did an Soap Handler with its handleFault method like this: public boolean handleFault(SOAPMessageContext context) {
我用它的 handleFault 方法做了一个 Soap 处理程序,如下所示: public boolean handleFault(SOAPMessageContext context) {
try {
System.err.println("Handler handleFault");
if (context.getMessage().getSOAPBody().hasFault()) {
SOAPFault fa = context.getMessage().getSOAPBody().getFault();
System.err.println(fa.getFaultString());
System.err.println("FaultCode: " + fa.getFaultCode() + " - Detail: " + fa.getDetail());
}
return true;
} catch (SOAPException ex) {
System.err.println("SoapEx " + ex.getMessage());
return true;
}
}
But in my output all I have is :
但在我的输出中,我所拥有的是:
Handler handleFault
Fault occurred while processing.
FaultCode: soap:Server - Detail: [detail: null]
How do I process the errors node?
如何处理错误节点?
Update:
更新:
with fa.getDetail().getFirstChild().getTextContent()
I get the text within the xml. How do I get that as an object. It be a WaybillRegistrationFault I think.
与fa.getDetail().getFirstChild().getTextContent()
我得到的XML中的文本。我如何将其作为对象。我认为这是一个 WaybillRegistrationFault。
采纳答案by omer.dogan
if you want to print fault content as a xml string , you must convert the dom instance returned by the fa.getDetail() to String , fa.getDetail().toString() return ["+getNodeName()+": "+getNodeValue()+"]" not the content of the xml.
如果要将故障内容打印为xml字符串,则必须将fa.getDetail()返回的dom实例转换为String,fa.getDetail().toString() return ["+getNodeName()+":"+ getNodeValue()+"]" 不是 xml 的内容。
try the following;
尝试以下方法;
String detailStr=dom2string(fa.getDetail())
System.err.println("FaultCode: " + fa.getFaultCode() + " - Detail: " + detailStr);
public static final String dom2string(Node node) {
try {
if (node == null) {
return null;
}
Transformer tf = transformerThreadLocal.get();
// Create writer
StringWriter sw = new StringWriter(buffer, Integer.MAX_VALUE);
StreamResult result = new StreamResult(sw);
// transform
tf.transform(new DOMSource(node), result);
return sw.getBuffer();
} catch (Exception e) {
throw new RuntimeException("Could not convert Node to string", e);
}
}
回答by Ravi kiran Nageli
This is the way that I always handle soap fault errors:
这是我总是处理soap错误错误的方式:
`StringWriter sw = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(
new DOMSource(soapFaultException.getFault()), new StreamResult(sw));
String xml = sw.toString();`