The parent:
@ExceptionHandler(NoUserException.class)
protected ModelAndView handleException(NoUserException e) {
Map<String, Object> model = new HashMap<String, Object>();
model.put(ModelKeys.HOST_KEY, "message");
return new ModelAndView("noAccess",model);
}
The child:
@ExceptionHandler(NoUserException.class)
protected void handleException(NoUserException e, HttpServletRequest request, HttpServletResponse response) throws IOException {
logger.error("Invalid user.");
respond(CLIENT_USER_ERROR,response);
}
Yes, I do need them to have different parameters and outputs.
You can't have two methods with the same exception handler, sorry, it just isn't supported. The code that resolves them does not discern between super-subclasses and consider the subclass "more specific." The code is in org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver
if you're interested. It would not be hard to make your own implementation of AbstractHandlerExceptionResolver
based on that one that does consider a method directly on the controller a more specific result than an inherited method.
edit: personal comment, I have found over time it is better for me to suppress the urge to use "inheritance as templating tool" when making Spring MVC annotation driven controllers. View code is inherently clunky and procedural at times, this is why we make a separate "view layer" in the first place. An overly-micro approach to DRY and "re-use" does not avail you here. If it violates Liskov Substitution Principle, I don't do it. YMMV of course.
Why not just delegate the implementation of handleException()
to another method?
// superclass
protected ModelAndView handleExceptionImpl(
NoUserException e,
HttpServletResponse response) {
Map<String, Object> model = new HashMap<String, Object>();
model.put(ModelKeys.HOST_KEY, "message");
return new ModelAndView("noAccess",model);
}
@ExceptionHandler(NoUserException.class)
protected ModelAndView handleException(
NoUserException e,
HttpServletResponse response) {
return handleExceptionImpl(e, response);
}
// subclass
@Override
protected ModelAndView handleExceptionImpl(
NoUserException e,
HttpServletResponse response) {
logger.error("Invalid user.");
respond(CLIENT_USER_ERROR,response);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With