Announcement Announcement Module
Collapse
No announcement yet.
Problems with Abstract Controller @ExceptionHandlers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    abstract class BaseController {
        @InitBinder
        public void initBinder(final WebDataBinder binder) {...}
    
        @ExceptionHandler(Exception.class)
        @ResponseBody
        public RestErrorResponse handleException(Exception ex) {...}
    }
    
    @Controller
    @RequestMapping("/controller1")
    public class Controller1 extends BaseController {}
    
    @Controller
    @RequestMapping("/controller2")
    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?

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