Points To Remember
There are three ways to do exception handling in spring boot application.
- Global Level using - @ControllerAdvice
- Controller Level using - @ExceptionHandler
- Method Level using - try/catch
How to do Exception Handling in Rest Application
To demonstrate Exception Handling we will make three Exceptions.
Add a class with annotation @ControllerAdvice and @RestController. This will make the class return a Rest Response.- 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
- @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
- package com.ekiras.controller;
- import com.ekiras.exception.BaseException;
- import com.ekiras.exception.CustomException1;
- import com.ekiras.exception.CustomException2;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * @author ekansh
- * @since 19/2/16
- */
- @RestController
- @RequestMapping({"","/"})
- public class HomeController {
- @RequestMapping("/ex1")
- public String ex1(){
- // will be catched by global exception handler method handleBaseException
- throw new BaseException("Base Exception");
- }
- @RequestMapping("/ex2")
- public String ex2(){
- // will be catched by global exception handler method handleBaseException
- throw new CustomException1();
- }
- @RequestMapping("/ex3")
- public String ex3(){
- // will be catched by global exception handler method handleBaseException
- throw new CustomException2();
- }
- @RequestMapping("/ex4")
- public String ex4(){
- // will be catched by global exception handler method handleException
- throw new NullPointerException("null pointer exception");
- }
- @RequestMapping("/ex5")
- public String ex5(){
- // will be catched by controller exception hnadler handler method nfeHandler
- throw new NumberFormatException("number format exception");
- }
- /**
- * This method will handle all the Number Format Exceptions that arise within this controller.
- *
- * */
- @ExceptionHandler(value = NumberFormatException.class)
- public String nfeHandler(NumberFormatException e){
- return e.getMessage();
- }
- }
GlobalExceptionHandler.class
- package com.ekiras.handler.exception;
- import com.ekiras.exception.BaseException;
- import org.springframework.http.HttpStatus;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseStatus;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * @author ekansh
- * @since 19/2/16
- */
- @ControllerAdvice
- @RestController
- public class GlobalExceptionHandler {
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- @ExceptionHandler(value = BaseException.class)
- public String handleBaseException(BaseException e){
- return e.getMessage();
- }
- @ExceptionHandler(value = Exception.class)
- public String handleException(Exception e){return e.getMessage();}
- }
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
- @ExceptionHandler(value = NumberFormatException.class)
- public String nfeHandler(NumberFormatException e){
- return e.getMessage();
- }
Comments
Post a Comment