Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC exception handling with HandlerExceptionResolver
    primarykey
    data
    text
    <p>I am currently trying to use <code>HandlerExceptionResolver</code> for exception handling in a Spring MVC project. </p> <p>I want to handle normal exceptions via <code>resolveException</code> as well as 404's via <code>handleNoSuchRequestHandlingMethod</code>.</p> <p>Depending on the request type JSON or text/html the exception response should be returned appropriately.</p> <p><code>resolveException</code> works now.</p> <p>But <code>handleNoSuchRequestHandlingMethod</code> is giving me a headache. It's never called!</p> <p>According to the docu the method should be called on 404 errors</p> <p><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html" rel="nofollow">http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html</a></p> <p>What am I doing wrong...</p> <p>This is what I have so far.</p> <pre><code>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; } } </code></pre> <p><strong>EDIT with DefaultHandlerExceptionResolver</strong>:</p> <pre><code>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; } } </code></pre> <p>The above code still has no effect.</p> <p>Any other ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload