Skip to main content

SpringBoot : How to do Exception Handling in Rest Application

Points To Remember

There are three ways to do exception handling in spring boot application.

  1. Global Level using -  @ControllerAdvice
  2. Controller Level using - @ExceptionHandler 
  3. Method Level using - try/catch  

How to do Exception Handling in Rest Application

To demonstrate Exception Handling we will make three Exceptions.

  • BaseException that will be parent of all the Custom Exceptions.
  • CustomException1 that extends Base Exception.
  • CustomException2 that extends Base Exception.
We will create the following methods
  • ex1() throws BaseException
  • ex2() throws CustomException1
  • ex3() throws CustomException2
  • ex4() throws NullPointerException 
  • ex5() throws NumberFormatException
and now we will see how we can handle these exceptions using above mentioned ways.

Way 1 : Global Exception Handling using @ControllerAdvice

Add a class with annotation @ControllerAdvice and @RestController. This will make the class return a Rest Response.


  • @ControllerAdvice tells your spring application that this class will do the exception handling for your application.
  • @RestController will make it a controller and let this class render the response.
  • Use @ExceptionHandler annotation to define the class of Exception it will catch. (A Base class will catch all the Inherited and extended classes)
  • You can set the response status for exception using @ResponseStatus annotation.

HomeController.class

  1. package com.ekiras.controller;  
  2.   
  3. import com.ekiras.exception.BaseException;  
  4. import com.ekiras.exception.CustomException1;  
  5. import com.ekiras.exception.CustomException2;  
  6. import org.springframework.web.bind.annotation.ExceptionHandler;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. /** 
  11.  * @author ekansh 
  12.  * @since 19/2/16 
  13.  */  
  14. @RestController  
  15. @RequestMapping({"","/"})  
  16. public class HomeController {  
  17.   
  18.     @RequestMapping("/ex1")  
  19.     public String ex1(){  
  20.         // will be catched by global exception handler method handleBaseException  
  21.         throw new BaseException("Base Exception");  
  22.     }  
  23.   
  24.     @RequestMapping("/ex2")  
  25.     public String ex2(){  
  26.         // will be catched by global exception handler method handleBaseException  
  27.         throw new CustomException1();  
  28.     }  
  29.   
  30.     @RequestMapping("/ex3")  
  31.     public String ex3(){  
  32.         // will be catched by global exception handler method handleBaseException  
  33.         throw new CustomException2();  
  34.     }  
  35.   
  36.     @RequestMapping("/ex4")  
  37.     public String ex4(){  
  38.         // will be catched by global exception handler method handleException  
  39.         throw new NullPointerException("null pointer exception");  
  40.     }  
  41.   
  42.     @RequestMapping("/ex5")  
  43.     public String ex5(){  
  44.         // will be catched by controller exception hnadler handler method nfeHandler  
  45.         throw new NumberFormatException("number format exception");  
  46.     }  
  47.   
  48.     /** 
  49.      * This method will handle all the Number Format Exceptions that arise within this controller. 
  50.      *  
  51.      * */  
  52.     @ExceptionHandler(value = NumberFormatException.class)  
  53.     public String nfeHandler(NumberFormatException e){  
  54.         return e.getMessage();  
  55.     }  
  56.   
  57. }  

GlobalExceptionHandler.class

  1. package com.ekiras.handler.exception;  
  2.   
  3. import com.ekiras.exception.BaseException;  
  4. import org.springframework.http.HttpStatus;  
  5. import org.springframework.web.bind.annotation.ControllerAdvice;  
  6. import org.springframework.web.bind.annotation.ExceptionHandler;  
  7. import org.springframework.web.bind.annotation.ResponseStatus;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. /** 
  11.  * @author ekansh 
  12.  * @since 19/2/16 
  13.  */  
  14. @ControllerAdvice  
  15. @RestController  
  16. public class GlobalExceptionHandler {  
  17.   
  18.     @ResponseStatus(HttpStatus.BAD_REQUEST)  
  19.     @ExceptionHandler(value = BaseException.class)  
  20.     public String handleBaseException(BaseException e){  
  21.         return e.getMessage();  
  22.     }  
  23.   
  24.     @ExceptionHandler(value = Exception.class)  
  25.     public String handleException(Exception e){return e.getMessage();}  
  26.   
  27.   
  28. }  

Here,
  • handleBaseException(BaseException e) ::  will catch all the exceptions for classes BaseException, CustomException1 and CustomException2.
  • handleException(Exception e) ::  will handle all the exceptions that are childs of Exception class.

Note :: If BaseException or its child exception is thrown then handleBaseException() will catch this exception and not the handleException() method

Way 2 : Controller Level Exception handling using @ExceptionHandler


  1. @ExceptionHandler(value = NumberFormatException.class)  
  2.  public String nfeHandler(NumberFormatException e){  
  3.      return e.getMessage();  
  4.  }  
The above code in the HomeController.java will catch will the NumberFormatException that arise from this controller. This will not handle NumberFormatException that occurs in any other controller.







Comments