2011-01-31 27 views
1

我目前正在開發基於EJB3的SOAP webservice,我想知道什麼是處理未捕獲的異常並向客戶端返回格式良好的SOAP響應的最佳實踐。如何處理基於ejb 3的soap web服務中的異常

例如:

@WebMethod 
public SomeResponse processSomeService(
     @WebParam(name = "someParameters") SomeParameters someParameters) 
{ 
    // the EJB do something with the parameters 
    // and retrieve a response fot the client 
    SomeResponse theResponse = this.doSomething(someParameters); 

    return theResponse; 
} 

我必須趕上通用的異常,如:

@WebMethod 
public SomeResponse processSomeService(
     @WebParam(name = "someParameters") SomeParameters someParameters) 
{ 
    // the EJB do something with the parameters 
    // and retrieve a response to return to the client 
    try 
    { 
     SomeResponse theResponse = this.doSomething(someParameters); 
    } 
    catch (Exception ex) 
    { 
     // log the exception 
     logger.log(Level.SEVERE, "something is going wrong {0}", ex.getMessage()); 
     // get a generic error response not to let the 
     // technical reason going to the client 
     SomeResponse theResponse = SomeResponse.createError(); 
    } 

    return theResponse; 
} 

是否有爲了實現這個某種「最佳實踐」的?

謝謝

回答

1

如果您想在發生錯誤時返回SOAP消息,則必須創建它。我不希望容器知道你需要什麼。

我會說你的設計是足夠的。我的一個批評是,你不能具體說明錯誤的性質。

0

沒有必要捕捉它們,ejb容器將處理這個。

由亞歷山大GUIDET關於註釋#1

從EJB 3.1規範,第二章14.2.2異常處理UPDATE - >系統異常:

  • 如果bean方法遇到 系統異常或錯誤,它應該 簡單地將錯誤從 bean方法傳播到容器(即, bean方法不必 捕捉異常)。

  • 如果bean方法執行 操作,結果在一個檢查 異常bean方法不能 恢復,bean方法應該拋出 一個包裝 原始異常javax.ejb.EJBException異常。

  • 任何其他意外錯誤條件 應該用 javax.ejb.EJBException異常報告。

因此,傳播RuntimeException到容器是EJB規範建議的方式。

+0

不幸的是,似乎某些異常沒有被ejb-container捕獲,並且通過SOAP響應返回給客戶端 – 2011-01-31 10:52:06

+0

@Alexandre:當然可以。這就是容器應該做的事情,將運行時異常作爲SOAPFault返回。 – 2011-01-31 18:13:33

2

在響應中返回錯誤並不是好習慣,SOAP定義了一個SOAP錯誤來處理錯誤返回。正如Michael Konietzka指出的那樣,容器可以處理拋出的異常並將其轉換爲SOAP錯誤。

就你而言,你似乎想捕獲並記錄異常 - 首先拋出一個新的EJBException封裝原始異常。