Exception Handling With @ControllerAdvice in Spring 3.2

In my previous article I have explained one of the new feature Spring MVC Test Fraemwork which is introduced as part of the Spring Framework 3.2 release. This tutorial explains another good feature for exception handling on Spring MVC web application. Spring 3.2 introduces new annotation @ControllerAdvice annotation used for defining the exception handler with specific exception details for each method If any exception thrown on any part of the application will be handled by this component. In the previous release, we have configured GlobalException handling elements in the configuration files. With this new feature, it becomes more simple to handle the exceptions. Lets look at the example below to know how to implement in your project.

ExceptionHandlingTestController.java


package javabeat.net;

import java.io.IOException;
import java.sql.SQLException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ExceptionHandlingTestController {
	@RequestMapping("/hello1")
	public String testException1() throws IOException{
		boolean exceptionFlag = true;
		if (exceptionFlag){
			throw new IOException();
		}
		return "hello";
	}
	@RequestMapping("/hello2")
	public String testException2() throws SQLException{
		boolean exceptionFlag = true;
		if (exceptionFlag){
			throw new SQLException();
		}
		return "hello";
	}
}

ControllerAdviceTest.java

package javabeat.net;

import java.io.IOException;
import java.sql.SQLException;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@ControllerAdvice
@EnableWebMvc
public class ControllerAdviceTest {
	@ExceptionHandler(IOException.class)
	public ModelAndView handleIOException(IOException exception){
		ModelAndView andView = new ModelAndView();
		andView.setViewName("error");
		return andView;
	}
	@ExceptionHandler(SQLException.class)
	public ModelAndView handleSQLException(SQLException exception){
		ModelAndView andView = new ModelAndView();
		andView.setViewName("error");
		return andView;
	}
}

In the above example I have included two classes,

  1. Controller – ExceptionHandlingTestController.java. Conrtoller class defines two method swith both are throwing different exceptions. For the testing purpose I have explicitly throuwing the specific exception from that method. Also declared exception handler in the method signature.
  2. There is no change in the controller classes. It is normal implementation and there is no extra configuration for mapping the exception handlers. That is awesome!!
  3. Controller Advice – ControllerAdviceTest.java. This is the new addition with Spring 3.2. It is important to note that I have annotated with two annotations @ControllerAdvice and @EnableWebMvc. @ControllerAdvice is new annotation for informing the spring container about the exception handler. Any class which is annotated with @ControllerAdvice will be registered as the global exception handler.
  4. Trust me, there is no other configuration in the XML files. It just works once you complete writing the controller service class.
  5. If you look at the controller advice class, I have defined different methods for each type of exceptions. When application throws that specific exception, it will redirect to the configured view. You can customize what ever way you want to have your error pages. It is very simple to add multiple error pages for each exception.

Lets try to execute this example and share your feedback about this new feature. I found this would help to simplify the exception handling the spring web applications. What is your ideas about this new feature?. Please write it in the comments section or tweet me.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*