Spring Boot Road Primer (9) --- global exception handler program

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Geffin/article/details/100170833

When the web program abnormality has occurred, they tend to show "Whitelabel Error Page" page directly in the client, very pretty. Therefore, each web application will need to ensure that the client can receive prompt and friendly through a unified global exception handler program.

1 Spring Boot default exception handling mechanism

When a browser client requests a page that does not exist or the server processing an exception occurs, Spring Boot will respond to a default html content of the document, called the "Whitelabel Error Page".

Let's write a controller, it generates an intentional error

@Controller
public class IndexController {

	@RequestMapping("index")
    public String index(){
		int i = 1/0;
        return "index" ;
	}
}

Then we open the browser, find the page as follows:
Here Insert Picture Description

2 to handle the exception annotations by @ControllerAdvice

We can use @ControllerAdvice annotations and notes @ExceptionHandler special treatment to achieve the specified exceptions. If we need a local exception handling, use @Controller + @ExceptionHandler can, if need global exception handler, you should use @ControllerAdvice + @ExceptionHandler.

Local Exception Handling

Local exception handling requires @ExceptionHandler comment to the method of the class, is thrown when an exception is defined in this annotation, this method will be executed. If the class is where @ExceptionHandler @Controller, this method only works in this category. If the class resides with @ControllerAdvice @ExceptionHandler notes, this method will act globally.

For example, we had to modify the controller uses the above, to catch exceptions using @ExceptionHandler

@Controller
public class IndexController {

	@RequestMapping("index")
    public String index(){
		int i = 1/0;
        return "index" ;
	}
	
	@ExceptionHandler(Exception.class) 
    @ResponseBody 
    public String exHandler(Exception e){ 
      if(e instanceof ArithmeticException){ 
          return "/ by zero"; 
      } 
      return "others"; 
    }
}

Then we open the browser again
Here Insert Picture Description
found that the exception has been processed @ExceptionHandler, and outputs an error message to the client.

Global exception handler

@ControllerAdvice add annotations to spring 3.2.

We need to pay attention to the error will only enter the Controller layer by the @ControllerAdvice processing, interceptors thrown errors and access errors addresses the situation will be handled by the SpringBoot default exception handling mechanism.

We create a global exception handler class

@ControllerAdvice
public class ExceptionAdvice {

	@ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map<String,Object> exceptionHandler(Exception ex){
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("message", ex.getMessage());
        map.put("localizedmessage", ex.getLocalizedMessage());
        map.put("class", ex.getClass());
        map.put("stacktrace", ex.getStackTrace());
        map.put("suppressed", ex.getSuppressed());
        return map;
     }

}

When the controller throws an exception, displayed in the client
Here Insert Picture Description

This paper simply introduces the global Spring Boot exception handling scheme, if the inadequacies of hope that we can point out in the comments, thank you for watching.

Guess you like

Origin blog.csdn.net/Geffin/article/details/100170833