我将以下代码部署到AWS弹性beanstalk:
@ControllerAdvice
public class FooControllerAdvice {
@ExceptionHandler(value = NotFoundException.class)
public ResponseEntity<RestError> handleNotFound(HttpServletRequest req, NotFoundException ex) {
RestError restError = ...
return new ResponseEntity<>(restError, HttpStatus.NOT_FOUND);
}
}
部署后,NotFoundException
被handleNotFound
正确捕获,并按预期返回404。但是,经过一段时间(约一小时)后,它将不再被handleNotFound
捕获,而是开始返回500。我不确定它是AWS EB问题还是Spring问题。以前有人遇到过类似的问题吗?请指教,谢谢!
更新:
经过进一步调查后,我怀疑它发生在每小时AWS EB日志轮换之后。
第一个日志从上午10点开始,有助于404:
2017-10-23_10:03:14.957 [qtp739973450-13] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver.resolveException - Resolving exception from handler ...
2017-10-23_10:03:14.958 [qtp739973450-13] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException - Invoking @ExceptionHandler method: ...
从上午11点开始的下一个日志,有助于500:
2017-10-23_11:45:38.990 [qtp739973450-14] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver.resolveException - Resolving exception from handler ...
2017-10-23_11:45:38.990 [qtp739973450-14] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver.resolveException - Resolving exception from handler ...
2017-10-23_11:45:38.991 [qtp739973450-14] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver.resolveException - Resolving exception from handler ...
看起来第二个没有设法找到@ExceptionHandler
方法而是使用了默认值。
更新
今天我有另一种奇怪的行为。在同一个类中有另一个方法返回409响应,如下所示:
@ExceptionHandler(value = DataExistsException.class)
public ResponseEntity<RestError> handleDataExists(HttpServletRequest req, DataExistsException ex) {
RestError restError = ...
return new ResponseEntity<>(restError, HttpStatus.CONFLICT);
}
部署后的一段时间,这个开始给500而不是409(Spring找不到这个ExceptionHandler
)。与此同时,handleNotFound
仍然正确地返回404 ...