Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC exception handling with HandlerExceptionResolver

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 via handleNoSuchRequestHandlingMethod.

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

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html

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?

like image 760
Jeremy S. Avatar asked Aug 29 '11 09:08

Jeremy S.


1 Answers

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

https://jira.springsource.org/browse/SPR-8837?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=72648#comment-72648

like image 191
Jeremy S. Avatar answered Dec 31 '22 16:12

Jeremy S.