Spring Boot(2.1)に@RestControllerAdvice
カスタム例外を処理するがあるとしましょう。
例外ハンドラーの呼び出し中に別の例外がスローされた場合はどうなりますか?Springに独自の応答本文を送信させるのではなく、それを処理する方法はありますか?
{
"timestamp": "2020-01-31T03:03:55.228+0000",
"status": 500,
"error": "Internal Server Error",
"message": "account: 2",
"path": "/myendpoint/2"
}
@RestControllerAdvice
public class MyExceptionHandler {
@Autowired
private MyOtherService otherService;
@ExceptionHandler(SomeCustomException.class)
public ResponseBody<Object> handleCustomException(SomeCustomException ex) {
otherService.doSomething(); // This might throw, say, a RuntimeException
return new ResponseBody<>(/* ... */);
}
}
ExceptionHandlerは障害バリアなので、別の例外をスローせずに例外を処理する必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加