Spring MVC exception handling with HandlerExceptionResolver
Solution 1
According to Juergen Hoeller from Spring, it isn't possible with the HandlerExceptionResolver
because it only works for sub-mapping e.g.
you have a controller mapped to /account/**
and accesss a method from acount where no mapping exists like /acount/notExists
than it should work.
I will open a JIRA improvement ticket for this functionality
EDIT:
JIRA ticket about this issue
Solution 2
handleNoSuchRequestHandlingMethod
isn't part of the HandlerExceptionResolver
interface, so just declaring a method of that name will do nothing. It's a protected method specific to DefaultHandlerExceptionResolver
, and is called from its resolveException
method (which is part of the interface):
if (ex instanceof NoSuchRequestHandlingMethodException) {
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, request, response, handler);
}
To reproduce the same functionality, you can either subclass DefaultHandlerExceptionResolver
and override the methods you need to, or you need to add a case in your resolveException
method that handles NoSuchRequestHandlingMethodException
.
Jeremy S.
My Name is Jeremy and im a 29 years old developer developer from Vienna, Austria. Most of my professional life was dedicated to stuff like Java (Hibernate, Spring) Php (CakePHP) JavaScript (jQuery, ExtJS) HTML/CSS MySQL and other IT topics.
Updated on June 05, 2022Comments
-
Jeremy S. almost 2 years
I am currently trying to use
HandlerExceptionResolver
for exception handling in a Spring MVC project.I want to handle normal exceptions via
resolveException
as well as 404's viahandleNoSuchRequestHandlingMethod
.Depending on the request type JSON or text/html the exception response should be returned appropriately.
resolveException
works now.But
handleNoSuchRequestHandlingMethod
is giving me a headache. It's never called!According to the docu the method should be called on 404 errors
What am I doing wrong...
This is what I have so far.
public class JsonExceptionResolver implements HandlerExceptionResolver { protected final Log logger = LogFactory.getLog(getClass()); public ModelAndView resolveException(HttpServletRequest request, if (exception instanceof NoSuchRequestHandlingMethodException) { return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) exception, request, response, handler); } ... } public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpServletRequest request, HttpServletResponse response, Object handler){ logger.info("Handle my exception!!!"); ModelAndView mav = new ModelAndView(); boolean isJSON = request.getHeader("Accept").equals("application/json"); if(isJSON){ ... }else{ .. } return mav; } }
EDIT with DefaultHandlerExceptionResolver:
public class MyExceptionResolver extends DefaultHandlerExceptionResolver { protected final Log logger = LogFactory.getLog(getClass()); @Override protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { logger.warn("An Exception has occured in the application", exception); logger.info("exception thrown " + exception.getMessage() ); if (exception instanceof NoSuchRequestHandlingMethodException) { return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) exception, request, response, handler); } ... return mav; } public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpServletRequest request, HttpServletResponse response, Object handler){ logger.info("Handle my exception!!!"); ModelAndView mav = new ModelAndView(); boolean isJSON = request.getHeader("Accept").equals("application/json"); if(isJSON){ ... }else{ ... } return mav; } }
The above code still has no effect.
Any other ideas?
-
Jeremy S. over 12 yearsI also tried it with inheriting from
DefaultHandlerExceptionResolver
, but in case of a 404 thedoResolveException
never gets called... did I missunderstand you?