Web服务中的java / soapfault问题

时间:2010-10-27 10:11:52

标签: java xml web-services dom jax-ws

我有以下问题: 我正在使用SAAJ进行Web服务。我有一个带有以下soap fault的SOAPMessage:

<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: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>

但如果我试图获得细节,我什么都没得到! 我这样做:

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());

印刷:
细节值为 null
有详细信息 详细说明:空 -
节点值: null
有人可以帮忙吗?为什么我不能得到SOAP错误的细节?它包含在SOAP消息中?

更新:正确打印代码和faulttring 即
代码:S:服务器
FaultString:显示java.lang.NullPointerException

由于

1 个答案:

答案 0 :(得分:1)

对于DOM元素,方法getNodeValue将始终返回nullgetTextContent会更好,但在您的情况下,所有信息都包含在嵌套元素的属性中。您必须使用如下代码遍历这些嵌套元素。请注意,我还没有测试过代码。

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"));
            }
        }
    }
}