2016-09-26 15 views
2

spring mvcで例外をキャプチャする最も良い方法はどれですか。私は春のMVCでの例外処理の実装を終了するようになっていません。spring mvcの例外処理

@ControllerAdviceを実装しました。私は例外を処理するために正しい方法で実装されているかどうか確認してください。

質問:私は、サービス層の例外を処理するにはどうすればよい

  1. 。私はそれをコントローラに、その後UIにスローする必要がありますか?使い方。

  2. どのように私はDAOのレイヤーで、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層とNUMBERFORMAT 例外のような他の例外にSQL 例外を処理するにはどうすればよい

コントローラで入力検証を使用することをお勧めします。それを使用すると、この種のエラーは発生しません。しかし、入力の検証がない場合は、例外をスローして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

助けていただきありがとうございます。 – bharathi