2017-06-05 5 views
2

Springブート1.5.3の@ControllerAdvice注釈付きExceptionHandlerで特有の状況が発生しています。例外をキャッチするのはデフォルトの例外ですが、カスタム例外をスローすると起動しません。Springブート@ControllerAdviceが部分的に動作していて、カスタム例外のために動作しません。

exceptionHandlerの:

@ControllerAdvice 
public class ResponseEntityExceptionHandler { 

@ExceptionHandler({ HttpMessageNotReadableException.class }) 
protected ResponseEntity<ErrorModel> handleInvalidJson(RuntimeException e, WebRequest request) { 
    return new ResponseEntity<ErrorModel>(new ErrorModel().message("Could not parse JSON."), HttpStatus.BAD_REQUEST); 
} 

@ExceptionHandler({ NumberFormatException.class }) 
protected ResponseEntity<ErrorModel> handleInvalidRequest(RuntimeException e, WebRequest request) { 
    return new ResponseEntity<ErrorModel>(new ErrorModel().message("Invalid request parameter."), HttpStatus.BAD_REQUEST); 
} 

@ExceptionHandler({ CannotCreateTransactionException.class }) 
protected ResponseEntity<ErrorModel> handleTransactionCreationException(RuntimeException e, WebRequest request) { 
    return new ResponseEntity<ErrorModel>(new ErrorModel().message("Error connecting to the database, please make sure it is still available."), HttpStatus.BAD_REQUEST); 
} 

@ExceptionHandler({ NotFoundException.class }) 
protected ResponseEntity<ErrorModel> handleApiException(RuntimeException e, WebRequest request) { 
    return new ResponseEntity<ErrorModel>(new ErrorModel().message(e.getMessage()), HttpStatus.NOT_FOUND); 
} 
} 

トップ3の例外はすべてキャッチされ処理彼らがすることになっているとして、しかし、下の例外は、デフォルトの春ブーツexceptionHandlerので扱われますます。 ...

public ResponseEntity<?> readActor(@ApiParam(value = "Used to identify a single actor.", required = true) @PathVariable("actor_id") Integer actorId, @RequestHeader("Accept") String accept) throws Exception { 
    Actor actor = actorRepository.findOne(actorId); 
    if (actor == null) { 
     throw new NumberFormatException(""); 
    } 
    return new ResponseEntity<Actor>(actor, HttpStatus.OK); 
} 

これらがうまく処理されます:

public ResponseEntity<?> deleteActor(@ApiParam(value = "Used to identify a single actor.", required = true) @PathVariable("actor_id") Integer actorId, @RequestHeader("Accept") String accept) throws Exception { 
Actor actor = actorRepository.findOne(actorId); 
if (actor == null) { 
    throw new NotFoundException(404, "Not found"); 
} 
actorRepository.delete(actorId); 
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT); 
} 

私はこのようなトップ例外の一つ投げて試してみた:それは、私はコントローラ内部でスローカスタム例外です

Tomcatのログも、このことを示しています

2017-06-05 11:30:20.080 INFO 9076 --- [   main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in responseEntityExceptionHandler 

例外:

public class NotFoundException extends ApiException { 
private int code; 
public NotFoundException (int code, String msg) { 
    super(code, msg); 
    this.code = code; 
} 
} 

例外は、この基底クラスから継承:カスタム例外がexceptionHandlerのことで検出を回避した理由について

public class ApiException extends Exception{ 
private int code; 
public ApiException (int code, String msg) { 
    super(msg); 
    this.code = code; 
} 
} 

任意のアイデア?

必要な場合は追加情報を提供していただきます。

答えて

1

この特定の場合、NotFoundExceptionはExceptionから継承するだけなので、RuntimeExceptionの代わりにExceptionを使用します。

さらに注目すべき事柄:

  • 例外の一般的な名前を使用している場合1あなたは正しいものを輸入しているかどうかを確認常に、@ExceptionHandler(Exception.class)

  • を使用することができ、すべての例外をキャッチします。

+0

私はあなたの提案を追加しました。これは、何らかの理由で、これはトップ3の例外を捕捉しますが、例外はありません。また、1つの関数がすべての例外をキャッチすることは、望ましい動作ではありません。私はむしろ今のところ特定のものをキャッチしたいので、カスタムエラーメッセージを返すことができます。 – user6058309

+0

それをチェックして、私は正しいNotFoundExceptionをインポートしています:/、私はまた、投げて、成功しないより一般的なApiExceptionをキャッチしようとしました。 – user6058309

+0

OK、私の最後の試みはhandleApiExceptionに代わりのRuntimeExceptionの例外を使用することです: 保護ResponseEntity handleApiException {新しいResponseEntity (新ErrorModel()メッセージ(e.getMessage())を返す (例外e、WebRequestクラスリクエスト)。 、HttpStatus.NOT_FOUND); – rainerhahnekamp

関連する問題