Open In App

Spring MVC – Exception Handling

Last Updated : 11 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Spring MVC

When something goes wrong with your application, the server displays an exception page defining the type of exception, the server-generated exception page is not user-friendly. Spring MVC provides exception handling for your web application to make sure you are sending your own exception page instead of the server-generated exception to the user. The @ExceptionHandler annotation is used to detect certain runtime exceptions and send responses according to the exception. In this article we’ll Spring Mvc project to show how to intercept and define exceptions, we’ll look at how to define method level as well as the class level exception.

Steps to Create the Application

First, create a maven project, we are using Eclipse IDE for this project. Now, search for webapp, as we are creating a web application. Choose to create maven while creating a new project and add a maven webapp archetype. Enter the group id and the artifact id for your project and click finish.

After clicking finish your project structure would look something like this:

The pom.xml is auto-created with any maven project, it defines all the dependencies required for the project. Make sure to add all the dependencies mentioned in this file.

XML




<?xml version="1.0" encoding="UTF-8"?>
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.gfg</groupId>
  <artifactId>SpringMvcExceptionHandling</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <name>SpringMvcExceptionHandling Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency
        <groupId>org.springframework</groupId
        <artifactId>spring-webmvc</artifactId
        <version>5.1.1.RELEASE</version
    </dependency
    <dependency
        <groupId>org.apache.tomcat</groupId
        <artifactId>tomcat-jasper</artifactId
        <version>9.0.12</version
    </dependency
    <dependency>   
        <groupId>javax.servlet</groupId>   
        <artifactId>servlet-api</artifactId>   
        <version>3.0-alpha-1</version>   
    </dependency
    <dependency
        <groupId>javax.servlet</groupId
        <artifactId>jstl</artifactId
        <version>1.2</version
    </dependency
  </dependencies>
 
  <build>
    <finalName>SpringMvcExceptionHandling</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>


The web.xml defines mapping with different URLs and servlets to handle requests for those URLs. 

XML




         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
     
    <servlet>
        <servlet-name>gfg</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/gfg-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>gfg</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>


 

 

The gfg-servlet.xml file handles all HTTP requests for the web applications. The component scan locates and allocated beans according to the defined annotation. The annotation-driven enable the spring annotation classes. The bean configuration helps in identifying and scanning the JSP located in the views folder. 

 

XML




    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/context \
                        http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:component-scan base-package="com.gfg" />
     <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <mvc:annotation-driven />
     
</beans>


 

 

The Student class in the com.gfg.model defines the student object with three objects firstName, lastName, and rollNo. Notice that we have kept the roll number as a string instead of an integer, this will help us to check for possible NumberFormat exceptions.

 

Java




package com.gfg.model;
 
public class Student {
 
    private String firstName;
    private String lastName;
    private String rollNo;
 
    public Student(String firstName, String lastName,
                   String rollNo)
    {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.rollNo = rollNo;
    }
 
    public Student() {}
 
    public String getFirstName() { return firstName; }
 
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }
 
    public String getLastName() { return lastName; }
 
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
 
    public String getRollNo() { return rollNo; }
 
    public void setRollNo(String rollNo)
    {
        this.rollNo = rollNo;
    }
}


 

 

The LoginController class in the com.gfg.controller package defines two methods, the showForm method defines a Get mapping and simply shows the student login form. The processForm method has two parameters of @ModelAttribute for students and a Model to set attributes in our view pages. The model parameter sets all the attributes to the view page. Remember, in the Student class we defined rollNo as a String. Now, we will parse it into int: this will help in catching two NumberFormat exceptions. If the string is empty or the string has alphabets for both it will send a NumberFormatException. Now, to catch that exception and handle it separately for better user experience we define a method numberformatHandler and annotate it with the @ExceptionHandler, and set the value to NumberFormatException.class. So, this was a way to handle Exception at the method level.

 

Java




package com.gfg.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.gfg.model.Student;
 
@Controller
public class LoginController {
     
    @RequestMapping("/login")
    public String showForm(Model theModel) {
        theModel.addAttribute("student", new Student());
        return "portal";
    }
     
    @RequestMapping("/welcome")
    public String processForm(@ModelAttribute("welcome") Student student, Model mod) {   
        mod.addAttribute("FirstName", student.getFirstName());
        mod.addAttribute("LastName", student.getLastName());       
        int n = Integer.parseInt(student.getRollNo());
        mod.addAttribute("RollNo", n);   
        return "welcome";
    }
     
      @ExceptionHandler(value = NumberFormatException.class)
    public String numberformatHandler(Model theModel) {       
        theModel.addAttribute("err", "NumberFormatException");
        return "error";
    }
}


The  MyExceptionHandler class in the com.gfg.errorhandler defines all the exceptions for our application so that for a different kind of exception the user sees a proper and simple error page. To make it available for all the classes in our project we just have to add the annotation @ControllerAdvice, this will advise spring MVC to use our exception method instead of server-generated pages. So, In this we have defines an Exception Handler at the class level.

Java




package com.gfg.errorhandler;
 
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
 
@ControllerAdvice
public class MyExceptionHandler {
     
    @ExceptionHandler(value = NullPointerException.class)
    public String nullPointerHandler(Model theModel) {
        theModel.addAttribute("err", "NullPointerException");
        return "error";
    }
     
    @ExceptionHandler(value = Exception.class)
    public String AnyOtherHandler() {
        return "error";
    }
     
}


The portal.jsp file in the views folder defines the Student login portal. 

HTML




<%@ taglib prefix="form" url="http://www.springframework.org/tags/form" %> 
 
 <html
<head
</head
<body
    <h1>Student Portal</h1>
    <form:form action="welcome" modelAttribute="student"
       <label>First name:</label>
         <form:input path="firstName" />
        <br><br
         
         <label>Last name:</label>
         <form:input path="lastName" /> 
        <br><br>   
         
         <label>Roll No:</label>
          <form:input path="rollNo" />
         <br><br>   
      
        <input type="submit" value="Submit" /> 
       
    </form:form
</body
</html>


The welcome.jsp page in the views folder defines the welcome page for our application.

HTML




<%@ taglib prefix="form" url="http://www.springframework.org/tags/form" %> 
 
 <html
<head
</head
<body
    <h1>Student Portal</h1>
    <form:form action="welcome" modelAttribute="student"
       <label>First name:</label>
         <form:input path="firstName" />
        <br><br
         
         <label>Last name:</label>
         <form:input path="lastName" /> 
        <br><br>   
         
         <label>Roll No:</label>
          <form:input path="rollNo" />
         <br><br>   
      
        <input type="submit" value="Submit" /> 
       
    </form:form
</body
</html>


The error.jsp page is a simple exception handler page that defines the name of the exception and informs the user about an exception.

HTML




<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Opps....</h1>
    <h1> ${err} Exception caused</h1>
</body>
</html>


 

 

After adding all the classes and configuration files your project would look something like this:

 

 

Now that we have completed our project, it’s time to run it on a tomcat server, just start the tomcat server and type http:localhost:8080/SpringMvcExceptionHandling/login

 

OutputOutputOutputOutputOutput

 

So, in this Spring MVC project, we defined exception handlers at the method as well as at class level and defined our own custom exception view page for better user experience.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads