2013-02-04 17 views
0
檢索AxisFault做的printStackTrace當以下

我的InvocationTargetException打印:樣本Java代碼來的InvocationTargetException

 

AxisFault 
faultCode: file.could.not.be.created 
faultSubcode: 
faultString: The File could not be created 
faultActor: 
faultNode: 
faultDetail: 
    {http://schemas.xmlsoap.org/soap/envelope/}Fault:file.already.existsFile already exists 

The File could not be created 
    at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222) 
    at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129) 
    at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087) 
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) 

我想找回的faultcode &從的InvocationTargetException faultString: file.already.exists 文件已經存在

我該怎麼做?

回答

1

我不確定爲什麼stacktrace看起來像這樣,並且沒有包含實際InvocationFailureException的跟蹤,但我認爲AxisFault直接包裝在InvocationFailureException中,然後展開它可能會有所幫助,例如,如:

try { 
     // here code which throws InvocationFailureException 
    } catch (InvocationFailureException e) { 
     Throwable rootCause = e.getRootCause(); 
     if (rootCause instanceof AxisFault) { 
      AxisFault axFault = (AxisFault)rootCause; 
      // now extract information, e.g. 
      axFault.getFaultDetails(); 
     } 
    } 

也許你甚至需要遞歸獲取根本原因,如果它不直接包裝。

+0

感謝您的快速回復,根據您的建議我試過這個: Throwable rootCause = ex.getCause();如果(rootCause instanceof org.apache.axis.AxisFault){ AxisFault axFault =(AxisFault)rootCause; Element [] eleArray = axFault.getFaultDetails(); (元素ele:eleArray){SOP(「ele:」+ ele); SOP(ele.getChildNodes()); } SOP(axFault.getFaultString()); SOP(axFault.getFaultCode()); SOP(axFault.getFaultReason()); } 但只有我得到的是:「該文件無法創建」 無法獲得故障字符串,即「文件已存在」 – user2040528

+0

Arf,我一直都很無知的事實,軸返回他們的錯誤爲DOM -elements。如果要提取包含錯誤消息的元素的子節點的文本,可能像那個幫助器那樣有用:[提取元素的所有文本子元素](http://www.java2s.com/Code /Java/XML/Extractalltextchildrenofanelement.htm)...如果有必要,將其重寫爲遞歸... –