2017-02-27 30 views
0

私は3Tierアーキテクチャ(プレゼンテーション、アプリケーション、ドメイン層)に従ってSpringMVCを使用してWebアプリケーションを開発しています。また、プレゼンテーション層にファサードサービスがあり、コントローラからアプリケーションサービスへの各リクエストはファサードサービス(Contorller - > FacadeService - > ApplicationService)を経由します。アプリケーション層またはドメイン層で例外が発生した場合は、UIに表示する必要があります。それが今実装されている方法です。ファサードサービスでの例外処理

コントローラ

@PostMapping("/password/change") 
public String processChangePasswordRequest(ChangePasswordForm form, BindingResult bindingResult){ 
    ChangePasswordReqStatus status = facadeService.requestChangePassword(
      form.getOld(), 
      form.getPassword() 
    ); 

    if(status == ChangePasswordReqStatus.PASSWORD_MISMATCH) 
     bindingResult.rejectValue("oldPassword", "password.mismatch", "Wrong password"); 
    return "change_password"; 

FacadeService

@Override 
public ChangePasswordReqStatus requestChangePassword(Password old, Password password) { 
    try{ 
     accountService.changePassword(old, password); 
    }catch (PasswordMismatchException ex){ 
     return ChangePasswordReqStatus.PASSWORD_MISMATCH; 
    } 
    return ChangePasswordReqStatus.OK; 
} 

しかし、私はファサードのサービスで例外をキャッチすることができますまたは多分よりよい解決策がある天気をわからないんだけど?

答えて

0

アカウントサービスによってスローされた例外が例外チェックされていない場合、より優れたクリーンなデザインは、例外をまったくキャッチしないことです。 ControllerAdviceを使用し、そこにあるすべての例外と応答ロジック(返信ステータスとメッセージなど)を処理します。

@ControllerAdvice 
class GlobalDefaultExceptionHandler { 
    public static final String DEFAULT_ERROR_VIEW = "error"; 

    @ExceptionHandler(value = Exception.class) 
    public ModelAndView 
    defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { 
    // If the exception is annotated with @ResponseStatus rethrow it and let 
    // the framework handle it - like the OrderNotFoundException example 
    // at the start of this post. 
    // AnnotationUtils is a Spring Framework utility class. 
    if (AnnotationUtils.findAnnotation 
       (e.getClass(), ResponseStatus.class) != null) 
     throw e; 

    // Otherwise setup and send the user to a default error-view. 
    ModelAndView mav = new ModelAndView(); 
    mav.addObject("exception", e); 
    mav.addObject("url", req.getRequestURL()); 
    mav.setViewName(DEFAULT_ERROR_VIEW); 
    return mav; 
    } 
} 

あなたはこのような何かを行うことができます