2017-10-30 10 views
-2

私はSpringブート1.5.7.RELEASEを使用してREST APIを作成しています。私のコントローラは受信データを検証し、MyRequestValidationExceptionをスローします。これは@ExceptionHandlerによって処理されます。@ExceptionHandlerがSpring * RestControllerの* after * finallyブロックを呼び出しました

しかし、私のコードをテストすると、 finally {}ブロックの後に@ExceptionHandlerが呼び出されて、が見つかることがわかりました。

これは正常な動作ですか、何か問題がありますか?最終的にブロックされた最後のものでなければならない?

@RestController 
public class MyAPI { 
@RequestMapping(value = "/login") 
public AuthenticationResponse authenticationRequest(HttpServletRequest servletRequest, @RequestBody AuthenticationRequest authenticationRequest) { 
    long startTime = System.currentTimeMillis(); 
    try { 
     this.logStartAPI(servletRequest, authenticationRequest); 
     ThreadLocalStaticClass.getThreadLocalHttpStatus().set(HttpStatus.OK); 

     . 
     . 
     . 

     // This throws a Runtime exception RequestValidationException 
     authenticationRequest.validate(); 

     . 
     . 
    } finally { 
     this.logEndAPI(servletRequest, startTime, ThreadLocalStaticClass.getThreadLocalHttpStatus().get()); 
     ThreadLocalStaticClass.getThreadLocalHttpStatus().remove(); 
    } 
} 

これは私のExceptionHandler(@ControllerAdviceクラス内)です。今、私はあなたがそのような行動を持っている理由を理解考える

@RestController 
public class MyAPI { 
@RequestMapping(value = "/login") 
public AuthenticationResponse authenticationRequest(HttpServletRequest servletRequest, @RequestBody AuthenticationRequest authenticationRequest) { 
try { 
    long startTime = System.currentTimeMillis(); 
    try { 
     this.logStartAPI(servletRequest, authenticationRequest); 
     ThreadLocalStaticClass.getThreadLocalHttpStatus().set(HttpStatus.OK); 

     . 
     . 
     . 

     // This throws a Runtime exception RequestValidationException 
     authenticationRequest.validate(); 

     . 
     . 
    } finally { 
     this.logEndAPI(servletRequest, startTime, ThreadLocalStaticClass.getThreadLocalHttpStatus().get()); 
     ThreadLocalStaticClass.getThreadLocalHttpStatus().remove(); 
    } 
} catch(MyRequestValidationException e) { 
    //handling of your exception 
} 
} 

@ExceptionHandler(value = { MyRequestValidationException.class }) 
protected ResponseEntity<Object> handleMyRequestValidationException(RuntimeException ex, WebRequest request) { 
    logger.error("RequestValidationException", ex); 
    ThreadLocalStaticClass.getThreadLocalHttpStatus().set(HttpStatus.BAD_REQUEST); 
    return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request); 
} 
+0

_throwing_コードの 'finally'ブロックの前にどのように呼び出されますか? –

+0

Spring MVCは '@ ExceptionHandler'メソッドを' ExceptionHandler'として登録します。 '@ RequestMapping'ハンドラメソッドを呼び出した後、例外をスローしたかどうかをチェックします。そうであれば、それを処理できる 'ExceptionHandler'を見つけて使用しようとします。それまでに、実行によりハンドラメソッドコードが残されました。 Springは必要な場所にコードを挿入しません。 –

答えて

2

@ExceptionHandlerは、すべてのあなたの方法に比べてtry-catchのようなものです、それは同様の動作します。

関連する問題