Announcement Announcement Module
Collapse
No announcement yet.
SpringMVC exception handling: getting rid of the ModelAndView infrastructure Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SpringMVC exception handling: getting rid of the ModelAndView infrastructure

    I currently develop a RESTful web application using the @MVC REST extensions (@RequestBody, @ResponseBody, messageConvertors...). My plan is avoid the ModelAndView infrastructure at all but I've got problem with exception handling.

    The obvious choice is using the HandlerExceptionResolver interface which unfortunately makes me return a ModelAndView instance.

    Another choice is using the @ExceptionHandler annotation, which supports returning @ResponseBody since 3.1 SPR-6902. However, there is a limitation that the exception handling method (annotated with @ExceptionHandler) must be located in the same controller which threw the exception. There is a resolved issue SPR-8124, 3.1M2 claiming exceptions can be resolved now from a method which is located in another controller but I have not been able to make it work. Has anybody been more lucky than me?

    So my question is: is there any good way to handle exceptions in a centralized way without using ModelAndView?

  • #2
    I have the same issue.
    Also tried @ExceptionHandler with 3.1.0.RC2.

    Comment


    • #3
      Hi Ifty,
      I'm using spring-MVC 3.2.2 and I can't see that the @ResponseBody is supported (in the exception handling context).
      As you can see at the DispatcherServlet.processHandlerException method:

      Code:
      
      
      		// Check registered HandlerExceptionResolvers...
      		ModelAndView exMv = null;
      		for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
      			exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
      			if (exMv != null) {
      				break;
      			}
      		}
      		if (exMv != null) {
      			if (exMv.isEmpty()) {
      				return null;
      			}
      			// We might still need view name translation for a plain error model...
      			if (!exMv.hasView()) {
      				exMv.setViewName(getDefaultViewName(request));
      			}
      			if (logger.isDebugEnabled()) {
      				logger.debug("Handler execution resulted in exception - forwarding to resolved error view: " + exMv, ex);
      			}
      			WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
      			return exMv;
      		}
      
      		throw ex;
      If you have found a way to handle exceptions resolving without using the ModelAndView, please post your solution.

      Comment

      Working...
      X