Spring MVC
Spring MVC provides several ways of handling exceptions that occurs during the execution of your application. Today we will discuss the use of ControllerAdvice for handling exceptions.
ControllerAdvice annotation is used to handle exceptions that arise/thrown from any of the controller that is defined in the application.
For ControllerAdvice to work , we need to define in the spring confirguration file, else the annotation will not recognized and hence the exception handling will not work.
lets create a small project to test this feature.
1. Create a Home Controller to show the home page and throw few exceptions that can be handled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.kscodes.exceptionhandling; import java.io.IOException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HomeController { @RequestMapping("/home") public String getHomePage() { return "home"; } @RequestMapping("/ioException") public void throwIOException() throws IOException { throw new IOException("IO Exception thrown from Home page"); } @RequestMapping("/genericException") public void throwException() throws Exception { throw new Exception("Exception thrown from Home page"); } } |
2. Create a ExceptionController and annotate with @ControllerAdvice.
In this class add 2 methods that will handle 2 different Exceptions. You can also handle any of your user defined exceptions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.kscodes.exceptionhandling; import java.io.IOException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.ModelAndView; @ControllerAdvice public class ExceptionController { @ExceptionHandler(IOException.class) public ModelAndView catchIOException(IOException e) { ModelAndView mav = new ModelAndView("exception"); mav.addObject("title", "This is a IO Exception"); mav.addObject("message", e.getMessage()); return mav; } @ExceptionHandler(Exception.class) public ModelAndView catchGenericException(Exception e) { System.out.println("In Generic Ex Controller"); ModelAndView mav = new ModelAndView("exception"); mav.addObject("title", "This is a Generic Exception"); mav.addObject("message", e.getMessage()); return mav; } } |
3. Create the home and exception jsp’s
1 2 3 4 5 6 7 8 9 10 |
<html> <body> <h1>Click the Below links to call the Exception Handler</h1> <br> <a href="ioException">Throw IO Exception</a> <br> <a href="genericException">Throw Generic Exception</a> </body> </html> |
1 2 3 4 5 6 7 8 |
<html> <body> <h1>${title}</h1> <br> ${message} </body> </html> |
4. The web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Sample Project</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> |
5. The dispatcher-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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="com.kscodes.exceptionhandling" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> |
6. Now build the project and run it.
Output
On clicking any if the links on the home page, an exception will be thrown from HomeController.
This exception will be handled by the ErrorController, which adds a message and redirects the page to exception.jsp
Download the Source
SpringMVC-ExceptionHandlingControllerAdvice.zip