简体   繁体   中英

Spring @ControllerAdvise stopped working some time after deployment to AWS EB

I have the following code deployed to AWS elastic 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);
    }
}

After deployment, the NotFoundException is caught by handleNotFound correctly and it returns 404 as expected. However, after some time (about one hour), it will no longer be caught by handleNotFound and it starts returning 500 instead. I'm not sure whether it is AWS EB issue or Spring issue. Anyone has faced similar issue before? Please advise, thanks!

Update:

After further investigation, I suspect it happened after hourly AWS EB log rotation.

The first log starting from 10 am which contributes to 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: ...

The next log starting from 11 am which contributes to 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 ...

It looks like the second one did not manage to find @ExceptionHandler method and used the default instead.

Update:

Today I had another strange behaviour. There's another method in the same class returning 409 response like below:

@ExceptionHandler(value = DataExistsException.class)
public ResponseEntity<RestError> handleDataExists(HttpServletRequest req, DataExistsException ex) {

    RestError restError = ...

    return new ResponseEntity<>(restError, HttpStatus.CONFLICT);
}

Some time after deployment, this one starts to give 500 instead of 409 (Spring cannot find this ExceptionHandler ). At the same time, handleNotFound is still returning 404 correctly...

事实证明,这实际上是Spring的一个错误 ,已在5.0 RC4中修复。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM