2016-09-26 114 views
2

這是在spring mvc中捕獲異常的最佳方法。我沒有得到在mvc中執行異常處理的端到端的事情。春天的異常處理mvc

我已經實現了@ControllerAdvice。你能否確認我是否已經以正確的方式處理異常情況?

問:

  1. 我如何處理服務層異常。我必須把它扔到控制器,然後到用戶界面?怎麼運行的。

  2. 我如何處理DAO層中的sql異常和其他異常像Numberformat異常?

代碼:

@RequestMapping(value = "/getDepositSearch", method = RequestMethod.POST) 
public String depositNumberData(
     @ModelAttribute("searchCondition") String searchCondition, 
     @ModelAttribute("searchText") String searchText, 
     final RedirectAttributes redirect, Model depositStatus, 
     HttpServletRequest request) { 

    String pageForward = null; 

    try { 
     List<MRPSDeposit> depositDetails = null; 
     if (!searchText.isEmpty()) { 
      depositDetails = mrpsDeposit.getDepositDetails(searchCondition, 
        searchText); 
     } 
     Map<String, String> searchList = new LinkedHashMap<String, String>(); 

     if (searchCondition.equals(ManagementConstants.DEPOSITDATEKEY)) { 
      searchList.put(ManagementConstants.DEPOSITDATEKEY, 
        ManagementConstants.DEPOSITDATEVALUE); 
     } else if (searchCondition.equals(ManagementConstants.DEPOSITNUMBERKEY)) { 

      searchList.put(ManagementConstants.DEPOSITNUMBERKEY, 
        ManagementConstants.DEPOSITNUMBERVALUE); 
     } else { 
      searchList.put(ManagementConstants.DEPOSITNUMBERKEY, 
        ManagementConstants.DEPOSITNUMBERVALUE); 
      searchList.put(ManagementConstants.DEPOSITDATEKEY, 
        ManagementConstants.DEPOSITDATEVALUE); 

     } 

     if (depositDetails.size() == 0) { 
      redirect.addFlashAttribute("flashMessage", 
        ManagementConstants.NORECORDFOUND); 
      pageForward = "redirect:/mrps/getDepositDetails"; 
     } else if (depositDetails.size() > 1) { 

      Map<String, Map<String, String>> search = new HashMap<String, Map<String, String>>(); 
      search.put("searchContent", searchList); 
      depositStatus.addAttribute("searchAllContents", search); 
      depositStatus.addAttribute("depositDetails", depositDetails); 

      pageForward = "multipleDepositDetails"; 

     } else { 

      Map<String, Map<String, String>> search = new HashMap<String, Map<String, String>>(); 

      search.put("searchContent", searchList); 
      depositStatus.addAttribute("searchAllContents", search); 
      depositStatus.addAttribute("depositDetails", depositDetails); 
      if (request.isUserInRole("ROLE_READ")) { 
       pageForward = "readDepositDetails"; 
      } else { 
       pageForward = "updateDepositDetails"; 
      } 
     } 

    } catch (InfoManagementException e) { 
     System.out.println("weee"+e); 
    } 
    return pageForward; 

} 

服務層:

@Override 
@Transactional(readOnly = true) 
public List<MRPSDeposit> getDepositDetails(String searchCondition, 
     String searchText) { 
    List<MRPSDeposit> mrpsDepositDetails = new ArrayList<MRPSDeposit>(); 
    /* try { */ 
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", 
      Locale.ENGLISH); 
    if (searchCondition.equalsIgnoreCase(ManagementConstants.DEPOSITNUMBERKEY)) { 
     System.out.println("finalal"); 
     mrpsDepositDetails = mrpsDepositDao.findByDepositNumber(
       searchCondition, Short.valueOf(searchText)); 
    } else { 
     try { 
      mrpsDepositDetails = mrpsDepositDao.findByDepositDate(
        searchCondition, formatter.parse(searchText)); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    return mrpsDepositDetails; 

} 

DAO層:

@Override 
public List<MRPSDeposit> findByDepositNumber(String searchCondition, 
     Short searchTxt) { 
    List<MRPSDeposit> searchResult = super.findByDepositNumber(
      searchCondition, searchTxt); 
    return searchResult; 
} 

控制器忠告:

@ControllerAdvice 
public class GlobalExceptionController { 

@ExceptionHandler(InfoManagementException.class) 
public ModelAndView handleCustomException(InfoManagementException ex) { 

    ModelAndView model = new ModelAndView("error/generic_error"); 
    System.out.println(); 
    model.addObject("errCode", ex.getErrCode()); 
    model.addObject("errMsg", ex.getErrMsg()); 

    return model; 

} 

@ExceptionHandler(Exception.class) 
public ModelAndView handleAllException(Exception ex) { 

    ModelAndView model = new ModelAndView("error/generic_error"); 
    model.addObject("errMsg", "this is Exception.class"); 

    return model; 

} 

}

+0

任何人都可以對此發表評論 – bharathi

回答

0

我如何處理服務層異常。我必須把它扔到 控制器,然後到UI?怎麼運行的。

在這兩種情況下,這取決於您的要求。在某些情況下,您需要顯示錯誤消息的同一頁面,並且在某些情況下,您需要重定向到另一個錯誤頁面。在其他情況下,您可能不需要顯示任何錯誤消息。

向控制器拋出異常然後在控制器建議中處理它並在UI上顯示可讀的錯誤消息是很常見的。在控制器建議中,您可以確定將顯示消息的頁面並記錄錯誤消息。

我該如何處理在DAO層的SQL 異常及其他異常喜歡NUMBERFORMAT 異常?

我會建議你在控制器中使用輸入驗證。如果你使用它,那麼你不會得到這種錯誤。但是,如果您沒有輸入驗證,則可以拋出異常並在UI上顯示消息。

更新

您可以留下您的服務層,你必須在這個時刻,處理在ControllerAdvice例外。如果你想在服務層中處理異常,你可以用try/catch來完成。

public void myServiceMethod(){ 
    try{ 
    ... 
    }catch(Exception1 e){//Every catch block can capture a group of exceptions. 
    //Depending on your business logic, you can throw a new Exception, log it,  or do some logic. 
    logger.log("My error: ", e); 
    }catch(Exception2 e){//Every catch block can capture a group of exceptions. 
    throw new MyBusinessException("Something ocurred", e); 
    } 
} 

然後在您的ControllerAdvice中,您需要處理MyBusinessException並執行您所需的操作。

+0

我可以舉例說明此服務層的異常處理。 – bharathi

+0

@bharathi我編輯了答案 – reos

+0

感謝您的幫助reos。 – bharathi