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:

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.

Create Controller

6) Create a controller named “HelloWorldController.java” in package org.arpit.java2blog.controller

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

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

packageorg.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

publicclassHelloWorldController{

privatestaticfinalLogger logger=Logger

.getLogger(HelloWorldController.class);

@RequestMapping("/helloworld/{helloType}")

publicModelAndView hello(@PathVariable("helloType")StringhelloType)

throws CustomException,IOException{

StringhelloWorldMessage="";

if(helloType.equalsIgnoreCase("CustomException")){

thrownewCustomException("Custom exception occured");

}elseif(helloType.equalsIgnoreCase("IOException")){

thrownewIOException(" IO exception occured");

}else{

helloWorldMessage="Hello world from java2blog!";

}

returnnewModelAndView("hello","message",helloWorldMessage);

}

@ExceptionHandler(CustomException.class)

publicModelAndView catchCustomException(CustomException ex){

ModelAndView model=newModelAndView("Custom_Exception");

model.addObject("errorMessage",ex.getMessage());

returnmodel;

}

@ExceptionHandler(IOException.class)

publicModelAndView catchIOException(IOException ex){

ModelAndView model=newModelAndView("IOException");

model.addObject("errorMessage",ex.getMessage());

returnmodel;

}

}

Create ControllerAdvice named GlobalExceptionController.java

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

packageorg.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

publicclassGlobalExceptionController{

@ExceptionHandler(CustomException.class)

publicModelAndView catchCustomException(CustomException ex){

ModelAndView model=newModelAndView("Custom_Exception");

model.addObject("errorMessage",ex.getMessage());

returnmodel;

}

@ExceptionHandler(IOException.class)

publicModelAndView catchCustomException(IOException ex){

ModelAndView model=newModelAndView("IOException");

model.addObject("errorMessage",ex.getMessage());

returnmodel;

}

}

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.