Summary of Java knowledge points: If you want to see it, you can enter it from here
Table of Contents
-
-
- 2.14. Exception handling
-
- 2.14.1, define the error page
- 2.14.2. Local abnormalities
- 2.14.3. Global exceptions
- 2.14.4, abnormal configuration class
- 2.14.5. Exception handling interface
- 2.14.6, priority and distinction
-
2.14, exception handling
2.14.1, define error page
SpringBoot’s default exception handling mechanism: Once an exception occurs in the program, SpringBoot will request the url of /error. In SpringBoot, a BasicExceptionController is provided to handle the /error request, and it will jump to the page that displays the exception by default to display the exception information.
However, we can add an error page in resources/templates/ to customize the exception page. The name and path cannot be changed, because BasicExceptionController jumps to this path by default.
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>Congratulations, you have entered the abnormal page</h1> </body> </html>
2.14.2, local exception
In SpringBoot, you can use the @ExceptionHandler
annotation to handle local exceptions. It defines a method in the Controller, internally writes the processing logic after the exception occurs, and then adds @ExceptionHandler(value={}) to the method Annotation indicates the exception to be caught, and then this method will be used to handle when an exception occurs in this Controller.
-
define an error page
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>Congratulations, you have entered the abnormal page</h1> <p th:text="${msg}"></p> </body> </html>
-
Controller: Do not write
@ExceptionHandler
@Controller @RequestMapping("/test") public class HelloController {<!-- --> @GetMapping("/hello") public String hello(String name){<!-- --> int i = 9/0; return "login"; } }
-
At this time, add the
@ExceptionHandler
annotation in the Controller, and complete the relevant methods, and handle it when an exception occurs@Controller @RequestMapping("/test") @Slf4j public class HelloController {<!-- --> @GetMapping("/hello") public String hello(String name){<!-- --> int i = 9/0; return "login"; } @ExceptionHandler(value = {<!-- -->Exception. class}) public String exceptionHandling(Model model,Exception e){<!-- --> model.addAttribute("msg","An exception occurred:" + e.getMessage()); log. info(e. getMessage()); return "error"; } }
2.14.3, global exception
The @ExceptionHandler
annotation is used to handle local exceptions, which are only valid in one Controller. We generally require global exception handling, which is valid for all Controllers.
Create a class that specifically handles exceptions, then use the @ControllerAdvice
annotation on the class, then create an exception handling method internally, and add the @ExceptionHandler
annotation to the method, this Classes are able to handle global exceptions.
-
Define a global exception handling class
@ControllerAdvice @Slf4j public class HandleException {<!-- --> @ExceptionHandler public String exceptionHandling(Model model, Exception e){<!-- --> model.addAttribute("msg","Global exception handling:" + e.getMessage()); log. info(e. getMessage()); return "error"; } }
-
Remove the original local exception method in Controller
@Controller @RequestMapping("/test") public class HelloController {<!-- --> @GetMapping("/hello") public String hello(String name){<!-- --> int i = 9/0; return "login"; } }
When using global exception handling, you can also use local exception handling, which has a higher priority than global exception handling. That is to say, after a Controller defines a local exception handling method, it will no longer execute global exception handling.
The above example adds a local exception handling method to the Controller
@Controller @RequestMapping("/test") public class HelloController {<!-- --> @GetMapping("/hello") public String hello(String name){<!-- --> int i = 9/0; return "login"; } @ExceptionHandler(value = {<!-- -->Exception. class}) public String exceptionHandling(Model model,Exception e){<!-- --> model.addAttribute("msg","local exception handling:" + e.getMessage()); return "error"; } }
2.14.4, abnormal configuration class
In addition to @ControllerAdvice
+ @ExceptionHandler
annotations for global exception handling, you can also create a SimpleMappingExceptionResolver object through the configuration class to handle exceptions, which is also global.
But using SimpleMappingExceptionResolver can’t get the exception object, can’t get the exception message, but it can judge multiple exception types in one method, and jump to different pages according to different exceptions.
@Configuration public class MyExceptionConfig {<!-- --> @Bean public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){<!-- --> System.out.println("handle exception"); SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver(); Properties properties = new Properties(); //Parameters: fully qualified exception type name, exception view name properties. put("java. lang. Exception","error2"); // properties.put() can write multiple, so that different exception types can jump to different exception pages resolver.setExceptionMappings(properties); return resolver; } }
Still use the above Controller, but note that both global and local exceptions need to be deleted, and SimpleMappingExceptionResolver has the lowest priority among the three.
2.14.5, exception handling interface
By implementing the HandlerExceptionResolver
interface, you can also handle exceptions. This processing method can not only get the exception object, but also jump to different pages according to different exceptions.
@Configuration public class MyHandlerExceptionResolver implements HandlerExceptionResolver {<!-- --> @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {<!-- --> ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("msg", "HandlerExceptionResolver interface handling exception: " + ex.getMessage()); //You can jump according to different exception types if(ex instanceof ArithmeticException){<!-- --> modelAndView.setViewName("error"); }else if(ex instanceof NumberFormatException){<!-- --> modelAndView.setViewName("error2"); } return modelAndView; } }
2.14.6, Priority and distinction
The priorities of the four kinds of exception handling are: @ExceptionHandler’s local exception handling> @ControllerAdvice
+ @ExceptionHandler
‘s global exception handling> HandlerExceptionResolver interface’s global exception handling> SimpleMappingExceptionResolver global exception deal with
@ExceptionHandler is local exception handling, which can only handle the exception of a single Controller, and the other three can handle global exceptions
HandlerExceptionResolver
and SimpleMappingExceptionResolver
can make different jumps for different types of exceptions in one method, while @ControllerAdvice
+ @ExceptionHandler
Global exception handling and @ExceptionHandler
local exception handling If you want to achieve different exception types and jump to different pages, you need multiple methods to achieve it.
SimpleMappingExceptionResolver cannot obtain exception objects, and the other three types can obtain exception objects