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

时间:2015-02-25 07:20:29

标签: java web-services soap

我正在尝试为web服务的soapFault resposne提取错误代码和错误消息,但是获取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>

Blow是要处理的代码:

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

              }
          }

      }
}

以下是输出:

errorCode:BSL-14004 errorMessage:OMS登录失败:OMS UAMS登录问题 - 嵌套异常/错误: java.net.ConnectException:尝试了所有:&#39; 1&#39;地址,但无法通过HTTP连接到服务器:&#39; 195.233.102.177&#39;,port:&#39; 40123&#39;

线程中的异常&#34;主线程&#34; java.lang.ClassCastException:com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl     at msisdn.getTariffsAndAddOns(msisdn.java:155)     在msisdn.main(msisdn.java:181)

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

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

1 个答案:

答案 0 :(得分:0)

您确定前两次获得TextImpl吗?显然,它失败了,因为它试图将强制转换TextImpl类型化为SoapElement。如果您只期望SoapElement类型,那么也许您可以检查

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