Spring MVC

Exception Handling in Spring MVC Example and Return Custom 404 Error Pages

Exception Handling in Spring MVC is about handling exceptions in the application by using spring framework. Spring offers we can customized our error page to more friendly error page to user. This example provides the steps to write your own 404 Resource Not Found error page for your application.
Here I am writing a custom exception for 404 error using the HttpStatus.NOT_FOUND. This will be annotated as the exception @ExceptionHandler in the controller. Whenever there is any error thrown on this type, it will be redirected to the configured error page.

Lets see the example.

1. Controller in Application

package com.doj.spring.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;

import com.doj.spring.web.bean.Student;
import com.doj.spring.web.exception.StudentNotFoundException;

@Controller
public class WebController {
 
 static Map<String, Student> map = new HashMap<String, Student>();
 @RequestMapping("/")
 public String home(){
  Student student = new Student();
  student.setFname("Dinesh");
  student.setLname("Rajput");
  student.setAge("29");
  student.setAddress("Noida");
  student.setCourse("MS");
  map.put("1000", student);
  return "home";
 }
 
 @RequestMapping("/doj-student-{rollNumber}")
 public String dojStudentByRollNumber(ModelMap model, @PathVariable(value="rollNumber") String rollNumber){
  Student student = map.get(rollNumber);
  if (student == null) {
   throw new StudentNotFoundException("Student Not Found", "ERROR:404");
  }
  String name = student.getFname()+" "+student.getLname()+" "+student.getAddress()+" "+student.getCourse();
  model.put("name", name);
  return "home";
 }
 
 @ExceptionHandler(StudentNotFoundException.class)
 public ModelAndView handleStudentNotFoundException(StudentNotFoundException ex) {
  Map<String, StudentNotFoundException> model = new HashMap<String, StudentNotFoundException>();
  model.put("exception", ex);
  return new ModelAndView("student.error.400", model);

 }
 
 @ExceptionHandler(Exception.class)
 public ModelAndView handleException(Exception ex) {
  Map<String, Exception> model = new HashMap<String, Exception>();
  model.put("exception", ex);
  return new ModelAndView("student.error.500", model);

 }
}


2. Add Custom Exception

package com.doj.spring.web.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="Student Not Found")
public class StudentNotFoundException extends RuntimeException{

 /**
  * 
  */
 private static final long serialVersionUID = -2581975292273282583L;
 
 String errorMessage;
 
 String errorCode;

 public StudentNotFoundException(String errorMessage, String errorCode) {
  super();
  this.errorMessage = errorMessage;
  this.errorCode = errorCode;
 }

 public String getErrorMessage() {
  return errorMessage;
 }

 public void setErrorMessage(String errorMessage) {
  this.errorMessage = errorMessage;
 }

 public String getErrorCode() {
  return errorCode;
 }

 public void setErrorCode(String errorCode) {
  this.errorCode = errorCode;
 }
 
}

3. Student Bean Class

package com.doj.spring.web.bean;


public class Student {
 
 String fname;
 
 String lname;
 
 String address;
 
 String course;
 
 String age;
 
 public String getFname() {
  return fname;
 }
 public void setFname(String fname) {
  this.fname = fname;
 }
 public String getLname() {
  return lname;
 }
 public void setLname(String lname) {
  this.lname = lname;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public String getCourse() {
  return course;
 }
 public void setCourse(String course) {
  this.course = course;
 }
 public String getAge() {
  return age;
 }
 public void setAge(String age) {
  this.age = age;
 }
 
}

4. Views JSP

home.jsp

View for Success Page

<h1>${name} Welcome to DOJ Classes for Spring MVC!!!</h1>

Error Page View
400.jsp

<h1>${exception.errorCode}  -  ${exception.errorMessage} !!!</h1>

mainTemplate.jsp

<!DOCTYPE h1 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<html>
 <head>
  <title><tiles:insertAttribute name="title"/></title>
  <style>
      .error {
          color: red; font-weight: bold;
      }
  </style>
 </head>
 <body style="text-align: center;">
  <div id="header" style="height: 10%;background-color: gray;"><tiles:insertAttribute name="header"/></div>
  <div id="body" style="height: 70%; background-color: aqua;padding-top: 40px;"><tiles:insertAttribute name="body"/></div>
  <div id="footer" style="height: 10%;background-color: gray;"><tiles:insertAttribute name="footer"/></div>
 </body>
</html>

5. Spring Configurations based on Java Configuration

package com.doj.spring.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages={"com.doj.spring.web.controller"})
public class SpringWebConfiguration extends WebMvcConfigurerAdapter{

 @Bean
 public ViewResolver viewResolver() {
  return new TilesViewResolver();
 }
 
 @Bean
 public TilesConfigurer tilesConfigurer() {
  TilesConfigurer tiles = new TilesConfigurer();
  tiles.setDefinitions(new String[] {
    "/WEB-INF/view/tiles/tiles-def.xml"
  });
  tiles.setCheckRefresh(true);
  return tiles;
 }
 
 //Configure for default static content handling
 @Override
 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  configurer.enable();
 }
}

6. Deployment Descriptor based on java configuration

package com.doj.spring.web;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import com.doj.spring.web.config.SpringWebConfiguration;

//this file is equivalent to web.xml
public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

 //Configuration for non web components like services, daos, repos, etc.
 @Override
 protected Class<?>[] getRootConfigClasses() {
  return null;
 }
 
 //Specifying Spring MVC configuration class "SpringWebConfiguration.class" it equivalent to *-servlet.xml file
 @Override
 protected Class<?>[] getServletConfigClasses() {
  return new Class<?>[]{SpringWebConfiguration.class};
 }

 //Mapping dispatcher server to "/" i.e. Servlet Mapping in the web.xml 
 @Override
 protected String[] getServletMappings() {
  return new String[]{"/"};
 }
}

All files related this example is not mention here for whole application you can download the zip of working application.

SpringMVCExceptionHandlingExample

7. Lets see demo this application

1. Welcome Page

http://localhost:8080/SpringMVCExceptionHandlingExample/

 

2. Error Page

http://localhost:8080/SpringMVCExceptionHandlingExample/doj-student-10001

 

 

 

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago