Spring 3 Web MVC – Exception Handling – incorporating AJAX

Spring 3 Web MVC Exception Handling with AJAX
Spring 3 Web MVC Exception Handling

This tutorial demonstrates how to handle exceptions in Spring 3 Web MCV.
Emphasis is given to Handling  exceptions that occur during AJAX requests. I.e. in the event of an exception during an AJAX request,  it is not desirable to redirect to an error page, instead JavaScript error handling code should be invoked with relevant information passed in.

 

A Netbeans project is available for download at the bottom of this page.

Scope

A few Spring 3 Web MVC Exception handling demonstrations are provided here:

  • Delegating exception-detection to the Spring framework but handling at as granular a level as desired through generic code.
  • Handling exceptions during AJAX requests, returning specific error messages to the client.
  • Handling specific and generic Exceptions at the @Controller level or at the global level.
  • Spring 3 Web MVC allows for very comprehensive Exception handling without all the boiler plate  try{...} catch{...}  code conventionally associated with error handling. I.e. Exception detection is delegated to the Spring framework. Handling the exception (or the specific Exception type) can be done generically.

Overview

Handling All Exceptions at the @Controller level

Methods can be added to any Controller to catch exceptions thrown within that controller.

Annotate an dedicated method within the @Controller with @ExceptionHandler() to have that method invoked when any Exception occurs within that @Controller (org.springframework.web.bind.annotation.ExceptionHandler)

Handling Specific Exceptions at the @Controller level

Note how a comma separated list of Specific exceptions can be specified between the {}’s in the ‘value’ attribute of @ExceptionHandler

In this instance, if either of those two Exception types (or sub-types) are thrown within any of the other @Controller methods, this @ExceptionHandler method is invoked.

Note that this handling is for this Controller only. Please continue reading for information in Global Exception Handling.

 

Handling All Exceptions or Specific Exceptions at the Global level

A global error handler can be declared in the application context (dispatcher-servlet.xml in the project available for download at the bottom of this page) as so. A popular and comprehensive handler from Spring is:  SimpleMappingExceptionResolver but in this example, I’ve extended this class to handle exceptions occurring during AJAX requests in a manor more conducive to ‘the displaying of meaningful popup messages‘.

Declaring an Exception Handler in the Application Context

ExceptionHandler extends SimpleMappingExceptionResolver. It’s three member variables are set in the application context. I.e.

 

Things to note here are:

  1. The exceptionMapping <property> declares that in the event of any RuntimeException (or sub-classes or RuntimeException) the page to be rendered is ‘unchecked.jsp’
  2. The declaration of the ‘AnnotationMethodHandlerExceptionResolver‘ bean with ‘p:order=”1″‘ means simply that @Controller methods annotated with @ExceptionHandler should be given priority 1 ahead (in this example) of other Exception handlers.
  3. Bean ‘com.outbottle.support.ExceptionHandler‘ is declared with ‘p:order=”2″‘ giving it lower priority than the other handler. It is also of course the class extending SimpleMappingExceptionResolver‘.
  4. messages.properties is declared, from which the ${ajaxErrorMessage} is taken. This properties file must be in the classpath.
  5. The <property> attributes are used to set matching member variables.

p:order

Note that only one handler is executed, they are not chained.

com.outbottle.support.ExceptionHandler

This class extends SimpleMappingExceptionResolver which extends ‘org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver’. The  method

is overridden to provide custom behaviour. This method is invoked when an Exception (not handled in the controller) is thrown.

Handling the Exception Globally

Following on from above, this is how the exceptions are handled…..

So, in short, set the  response.setStatus to some error code thus allow the AJAX error handling code to be invoked. Secondly, return to some view which simply prints out the error messages passed in. The error handling code is as so:

Note that in this example this excellent jQuery AJAX form plugin is used.

Conclusion

So that’s it. This tutorial has demonstrated

  1. How to handle Exceptions at a global level.
  2. How to handle Exceptions that occur during AJAX requests in a way suitable for displaying popup messages with the client side error handling JavaScript code.
  3. How to override the global exception handler set in the application-context thus, catching and handling specific exceptions at the @Controller level.

 Download


A simple Netbeans project is available for download below which demonstrates everything discussed in this article.

Comments welcome…..

6 Comments