2013-02-05 177 views
0

我嘗試建立wsdl客戶端jre5JAX-WS RI 2.1.3它的我的第一個expirience。我genereted班,wsdl2java工具從cxf並寫了包裝類各地的客戶的樣子:jaxws異常/錯誤處理

public class RequestHelper { 
    private DataLoadService service = new DataLoadService(); 
    private DataLoadServiceSoap client; 
    private static String token; 

    //....my constructor.... 

    public void sendData(data){ 
    try{ 
     if (tokenIsExpired()){ 
      renewToken(); 
     } 
     client.sendData(data, this.token); 
     }catch(SOAPFaultException e){ 
      //...work with e 
     } 
    } 
} 

我不明白我怎麼能在sendData方法來處理異常。
我的意思是,例如,在HTTP中我們有status codes,我們可以讀取狀態碼並確定從服務器獲得哪種類型的錯誤以及我們如何處理它們。
在我的情況下,我有令牌過期時間的問題。有時候,sendData請求會長時間傳送到服務器。並且當請求已經在服務器上時,令牌不再有效,然後我收到帶有短信「令牌過期」的異常。我想趕上單獨這種類型的異常,財產以後這樣的:

public class RequestHelper { 
private DataLoadService service = new DataLoadService(); 
private DataLoadServiceSoap client; 
private static String token; 

//....my constructor.... 

    public void sendData(data){ 
    try{ 
     if (tokenIsExpired()){ 
      renewToken(); 
     } 
     client.sendData(data, this.token); 
     }catch(SOAPFaultException e){ 
     //...work with e 
     }catch(TokenExpiredException e){ 
     renewToken(); 
     client.sendData(data, this.token); 
     } 
    } 
} 

我怎麼能與JAX-WS RI 2.1.3圖書館實現這一目標?

UPD:

} catch (SOAPFaultException e) { 
SOAPFault f = e.getFault(); 
f.getFaultString() //yes here we have error description with "Token" 
        //but with locals dependency, this is not safe handle exception by this value 
f.getFaultCode() //here simply string "soap:Receiver", do not know how i can recognize only "token exceptions" 
} 

回答

0

找出什麼是被返回從服務器SOAPFaultException的一部分。如果Exception包含錯誤信息,那麼我們可以寫下類似的東西。注意:錯誤代碼將是處理此問題的最佳方法。

try{ 
     if (tokenIsExpired()){ 
      renewToken(); 
     } 
     client.sendData(data, this.token); 
     }catch(SOAPFaultException e){ 
      if(e.getFault().getFaultString().equalsIgnoreCase("Token expired")) { 
       renewToken(); 
       client.sendData(data, this.token); 
      } 
      ...... 
     }    

另一種方式是從有錯誤代碼和錯誤消息的服務器拋出定製的SOAP異常和處理,在代碼

+0

請看到我的更新,我沒有訪問服務器端代碼。謝謝你的建議 – Mrusful