java/ soapfault problem in web services
I have the following problem: I am using SAAJ for web services. I have a SOAPMessage with the following soap fault:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns3="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>java.lang.NullPointerException</faultstring>
<detail>
<ns2:开发者_JS百科exception xmlns:ns2="http://jax-ws.dev.java.net/" class="java.lang.NullPointerException" note="To disable this feature, set com.sun.xml.internal.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system property to false">
<ns2:stackTrace>
<ns2:frame class="myClass" file="HandlerFile.java" line="261" method="invoke" /><ns2:frame class="myClass" file="HandlerFile.java" line="1" method="invoke" /><ns2:frame class="com.sun.xml.internal.ws.api.server.InstanceResolver$1" line="unknown" method="invokeProvider" /></ns2:stackTrace>
</ns2:exception>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
But if I try to get the details I get nothing!
I do it as follows:String code = soapMsg.getSOAPBody().getFault().getFaultCode();
String faultString = soapMsg.getSOAPBody().getFault().getFaultString();
Detail detail = reply.getSOAPBody().getFault().getDetail();
if(detail == null)
System.out.println("No detail");
else
System.out.println("Detail value is "+detail.getNodeValue());
Iterator it = reply.getSOAPBody().getFault().getDetail().getDetailEntries();
StringBuilder details = new StringBuilder();
details.append("Detail:");
while(it.hasNext())
{
System.out.println("Has details");
DetailEntry temp = (DetailEntry) it.next();
System.out.println(temp.getTextContent());
System.out.println(temp.getValue());
details.append(temp.getValue());
details.append("-");
Iterator it2 = temp.getChildElements();
while(it2.hasNext())
{
Node t = (Node) it2.next();
System.out.println("Node value:"+t.getNodeValue());
}
}
System.out.println("Details:"+details.toString());
It is printed:
Detail value is null Has details Details:null- Node value:null Can someone help on this? Why can I not get the details of SOAP fault? It is included in the SOAP message?UPDATE: The code and the faultstring is printed correctly.
I.e. code:S:Server FaultString:java.lang.NullPointerExceptionThanks
The Method getNodeValue
will always return null
for a DOM element, getTextContent
would be better but in your case all information is contained in attributes of nested elements. You would have to traverse these nested elements with code like the following. Please note that I haven't tested the code yet.
String uri = "http://jax-ws.dev.java.net/";
QName qnException = new QName(uri, "exception");
QName qnStackTrace = new QName(uri, "stackTrace");
QName qnFrame = new QName(uri, "frame");
SOAPFault fault = soapMsg.getSOAPBody().getFault();
System.out.println("code=" + fault.getFaultCode());
System.out.println("faultString=" + fault.getFaultString());
for (Iterator i=fault.getDetail().getDetailEntries(); i.hasNext(); ) {
DetailEntry detailEntry = (DetailEntry)i.next();
for (Iterator j=detailEntry.getChildElements(qnException); j.hasNext(); ) {
SOAPElement exception = (SOAPElement)j.next();
System.out.println("exception class=" + exception.getAttribute("class"));
for (Iterator k=exception.getChildElements(qnStackTrace); k.hasNext(); ) {
SOAPElement stackTrace = (SOAPElement)k.next();
for (Iterator l=stackTrace.getChildElements(qnFrame); l.hasNext(); ) {
SOAPElement frame = (SOAPElement)l.next();
System.out.println(" class=" + frame.getAttribute("class"));
System.out.println(" file =" + frame.getAttribute("file"));
}
}
}
}
精彩评论