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/


Exception Handling in Spring MVC


2. Error Page

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


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




No comments:

Post a Comment