java.lang.ClassCastException 从 SOAPFault 提取错误消息时

java.lang.ClassCastException while extracting error message from SOAPFault

我正在尝试提取 web 服务的 soapFault 响应的错误代码和错误消息,但收到 ClasscasrException:

以下是网络服务响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header xmlns:com="http://com.amdocs.bss.bsl/" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
      <bsl:BSLResponseHeader xmlns:bsl="http://com.amdocs.bss.bsl/">
         <ResponseTimestamp>2015-02-24T11:34:03.419+01:00</ResponseTimestamp>
         <MessageID>1234</MessageID>
         <BSLServiceOperation>getTariffAndAddOns getTariffAndAddOns</BSLServiceOperation>
      </bsl:BSLResponseHeader>
   </soapenv:Header>
   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>soapenv:Server</faultcode>
         <faultstring>BSL exception</faultstring>
         <detail xmlns:com="http://com.amdocs.bss.bsl/" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
            <bsl:error xmlns:bsl="http://com.amdocs.bss.bsl/">
             <errorCode>BSL-14004</errorCode>
          <errorMessage>OMS login failure: Problem in OMS UAMS login - 
Nested Exception/Error:
java.net.ConnectException: Tried all: '1' addresses, but could not connect over HTTP to server: '195.233.102.177', port: '40123'
</errorMessage>
          <detail>
            <ImplRetrieveCustomerAssignedProductRestOutput>
              <transactionId>1424678882788</transactionId>
              <?xml-multiple errorInfo?>
              <errorInfo>
                <errorCode>14004</errorCode>
                <errorMessage>OMS login failure: Problem in OMS UAMS login - 
Nested Exception/Error:
java.net.ConnectException: Tried all: '1' addresses, but could not connect over HTTP to server: '195.233.102.177', port: '40123'
</errorMessage>
                <sourceSystem>MCSS</sourceSystem>
              </errorInfo>
            </ImplRetrieveCustomerAssignedProductRestOutput>
            <httpstatusCode>500</httpstatusCode>
          </detail>
            </bsl:error>
         </detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

吹是代码处理:

catch (SOAPFaultException e) {


      SOAPFault fault=e.getFault();
      Detail soapfaultdeDetail= fault.getDetail();
      Iterator detailEntries = soapfaultdeDetail.getDetailEntries();
      System.out.println("kk");
      while(detailEntries.hasNext()){
          DetailEntry newEntry = (DetailEntry)detailEntries.next(); 
          if( newEntry!=null ){
              final Iterator childElementsIter =               newEntry.getChildElements();
              while ( childElementsIter.hasNext() ) {


                  final SOAPElement soapElement = (SOAPElement) childElementsIter.next();  
                  if(soapElement.getElementName().getQualifiedName()!=null &&  soapElement.getElementName().getQualifiedName().equalsIgnoreCase("errorMessage") ){
                      String name=soapElement.getElementName().getQualifiedName();
                      String value = soapElement.getValue();
                      System.out.println(name +" : "+value);
                  } else if(soapElement.getElementName().getQualifiedName()!=null && soapElement.getElementName().getQualifiedName().equalsIgnoreCase("errorcode")){
                      String name=soapElement.getElementName().getQualifiedName();
                      String value = soapElement.getValue();
                      System.out.println(name +" : "+value);
                      } 

              }
          }

      }
}

下面是输出:

错误代码:BSL-14004 errorMessage : OMS login failure: Problem in OMS UAMS login - 嵌套 Exception/Error: java.net.ConnectException:已尝试全部:“1”个地址,但无法通过 HTTP 连接到服务器:“195.233.102.177”,端口:“40123”

线程中的异常 "Main Thread" java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl 在 msisdn.getTariffsAndAddOns(msisdn.java:155) 在 msisdn.main(msisdn.java:181)

final SOAPElement soapElement = (SOAPElement) childElementsIter.next();(这是第155行

从错误位置看,前两个条件执行成功,第三次执行时抛出异常。有人可以建议我应该怎么做才能避免这种情况吗?

您确定前两次获得的是 TextImpl。显然,它失败了,因为它试图将 TextImpl 类型转换为 SoapElement。如果您只需要 SoapElement 类型,那么也许您可以检查

if (childElementIter.next() instanceof SoapElement) 
{ 
    // business logic 
}