• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Exception Handling With @ControllerAdvice in Spring 3.2

October 14, 2013 //  by Krishna Srinivasan//  Leave a Comment

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.

  • Read : Exception Handling in Spring by Manisha

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.

Category: Spring FrameworkTag: Spring Framework

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.

Previous Post: « Introduction To Spring MVC Test Framework
Next Post: Using Decoder With WebSocket In Java EE 7 »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact