Announcement Announcement Module
No announcement yet.
Problems with Abstract Controller @ExceptionHandlers Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with Abstract Controller @ExceptionHandlers

    I've run into a funny problem where some derived controller classes do not have the parent's @ExceptionHandler applied. Here was the code in question:

    abstract class BaseController {
        public void initBinder(final WebDataBinder binder) {...}
        public RestErrorResponse handleException(Exception ex) {...}
    public class Controller1 extends BaseController {}
    public class Controller2 extends BaseController {}
    In this simplified example, Controller1 uses the parent's @ExceptionHander and Controller2 mysteriously ignores it and uses the DefaultHandlerExceptionResolver instead. I ran the debugger all the way into the guts of HandlerMethodSelector.selectMethods and found that for the classes which failed ReflectionUtils would find a Controller2.handleException != BaseController.handleException. Bizarre! I made the @ExceptionHandler final and that did the trick, but if I tried to make initBinder final, I'd get a Verify exception about redefining a final method. Double bizarre!

    Can you spot the bug?

    public abstract class BaseController {...}
    So I'm posting this in case anyone else ever descends down this particular rathole.