Spring MVC Exceptional Handling using @ControllerAdvice example

In this post , we will see how to do exceptional handling in Spring MVC using @ControllerAdvice. In previous post, we have already seen how to use @ExceptionHandler to handle exceptions but @ExceptionHandler can be applied to one controller only but what if you want to handle exception globally i.e. across multiple controller. You can use @ControllerAdvice to handle exceptions globally.
Lets understand this with the help of example:

1) Create a dynamic web project using maven in eclipse named "SpringMVCControllerAdviceExample"

Maven dependencies 

2) Our pom.xml will look like below
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.arpit.java2blog</groupId>
  <artifactId> SpringMVCControllerAdviceExample</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name> SpringMVCControllerAdviceExample Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>
   <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
             <version>2.4.1</version>
        </dependency>
 </dependencies>
 <build>
  <finalName> SpringMVCControllerAdviceExample</finalName>

  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
     <source>${jdk.version}</source>
     <target>${jdk.version}</target>
    </configuration>
   </plugin>
  </plugins>

 </build>
 <properties>
  <spring.version>4.2.1.RELEASE</spring.version>
  <jdk.version>1.7</jdk.version>
 </properties>
</project>

Spring application configuration:

3) Change web.xml as below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">
 <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>  
   <welcome-file>index.jsp</welcome-file>  
 
  </welcome-file-list>
 <servlet>
 <servlet-name>springrest</servlet-name>
 <servlet-class>
  org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
 <servlet-name>springrest</servlet-name>
 <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>
4) create a xml file named springrest-servlet.xml in /WEB-INF/ folder.
Please change context:component-scan if you want to use different package for spring to search for controller.Please refer to spring mvc hello world example for more understanding.

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <mvc:annotation-driven/>
<context:component-scan base-package="org.arpit.java2blog.controller" />
 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>
 <mvc:default-servlet-handler/>
</beans>

Create Controller 

6) Create a controller named "HelloWorldController.java" in package org.arpit.java2blog.controller
package org.arpit.java2blog.controller;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.arpit.java2blog.exception.CustomException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

 private static final Logger logger = Logger
   .getLogger(HelloWorldController.class);

 @RequestMapping("/helloworld/{helloType}")
 public ModelAndView hello(@PathVariable("helloType") String helloType)
   throws CustomException, IOException {

  String helloWorldMessage="";
  if (helloType.equalsIgnoreCase("CustomException")) {
   throw new CustomException("Custom exception occured");
  } else if (helloType.equalsIgnoreCase("IOException")) {
   throw new IOException(" IO exception occured");
  } else {
   helloWorldMessage = "Hello world from java2blog!";
   
  }

  return new ModelAndView("hello", "message", helloWorldMessage);
 }

 @ExceptionHandler(CustomException.class)
 public ModelAndView catchCustomException(CustomException ex) {

  ModelAndView model = new ModelAndView("Custom_Exception");
  model.addObject("errorMessage", ex.getMessage());
  return model;
 }
 
 @ExceptionHandler(IOException.class)
 public ModelAndView catchIOException(IOException ex) {

  ModelAndView model = new ModelAndView("IOException");
  model.addObject("errorMessage", ex.getMessage());
  return model;
 }
}

Create ControllerAdvice named GlobalExceptionController.java

package org.arpit.java2blog.controller;

import java.io.IOException;

import org.arpit.java2blog.exception.CustomException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class GlobalExceptionController {

 @ExceptionHandler(CustomException.class)
 public ModelAndView catchCustomException(CustomException ex) {

  ModelAndView model = new ModelAndView("Custom_Exception");
  model.addObject("errorMessage", ex.getMessage());
  return model;
 }
 
 @ExceptionHandler(IOException.class)
 public ModelAndView catchCustomException(IOException ex) {

  ModelAndView model = new ModelAndView("IOException");
  model.addObject("errorMessage", ex.getMessage());
  return model;
 }
}

As you can see, we have annotated two method i.e. catchCustomException and catchIOException with @ExceptionHandler. These methods will handle CustomException and IOException respectively and these exceptions can be thrown by multiple controllers too.

Create view


Modify index.jsp as below
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HelloWorld</title>
</head>
<body>
Hello World!!
</body>
</html>

Create Custom_Excepion.jsp in /WEB-INF/ folder to handle CustomException exception.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Custom Exception</title>
</head>
<body>
${errorMessage}
</body>
</html>
Create IOException.jsp in /WEB-INF folder as below to handle IOException.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>IO Exception</title>
</head>
<body>
${errorMessage}
</body>
</html>

7) It 's time to do maven build.
Right click on project -> Run as -> Maven build
Maven build in eclipse

8) Provide goals as clean install (given below) and click on run
Maven build in eclipse

Run the application

9) Right click on project -> run as -> run on server
Select apache tomcat and click on finish
10) Lets hit below URL
http://localhost:8080/SpringMVCControllerAdviceExample/helloworld/hello


As you can see, it is working fine with above URL.
Now lets hit some URL which will throw exception and exception will be handled by @ExcpetionHandler in @ControllerAdvice class.

When you hit below URL.
http://localhost:8080/SpringMVCControllerAdviceExample/helloworld/CustomException


As you can see, when you hit above URL , CustomException is thrown and handled by catchCustomException method in @ControllerAdvice class.

When you hit below URL.
http://localhost:8080/SpringMVCControllerAdviceExample/helloworld/IOException




As you can see, when you hit above URL , CustomException is thrown and handled by catchIOException method in @ControllerAdvice class.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012