Spring Boot (3)
error handling
1. User defined exception handling, return to the specified interface
In the method of controller error:
@RequestMapping("/error") public String error() { String str=null; str.length(); return "test"; }
In custom error page.html:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> //Error <span th:text="${#execInfo}"></span> </body> </html>
2. Catch exception, ExceptionHandler handling method
Exception in method
@RequestMapping("/error") public String error() { String str=null; str.length(); return "test"; }
1) Handler handler ExceptionHandler catches the specified exception
@ExceptionHandler(value = {java.lang.NullPointerException.class}) public ModelAndView excuteException(Exception e){ ModelAndView mv=new ModelAndView(); mv.addObject("error",e.toString()); mv.setViewName("error1"); return mv; }
2) jump to error1 view
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> error1 Interface <span th:text="${#execInfo}"></span> </body> </html>
3. Global exception handling -- SimpleMappingExceptionResolver class
1) by creating a class and adding @ Configuration+@Bean annotation, you can register this bean when springboot starts
Load the bean when SpringBoot starts, and return to the specified interface in case of specified error
@Configuration public class GlobalException { @Bean public SimpleMappingExceptionResolver getExceptionResolver(){ SimpleMappingExceptionResolver resolver=new SimpleMappingExceptionResolver(); Properties mappings=new Properties(); /* * Parameter 1: type of exception, full name of exception type * Parameter 2: view name * */ mappings.put("java.lang.NullPointerException","error1"); resolver.setExceptionMappings(mappings); return resolver; } }
2) wrong method
@RequestMapping("/error") public String error() { String str=null; str.length(); return "test"; }
4. User defined exception class HandlerExceptionResolver
1) implement the methods in the HandlerExceptionResolver interface
@Configuration public class GlobalException implements HandlerExceptionResolver { /*Error type judgment on resolveException object*/ @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { ModelAndView mv=new ModelAndView(); if (e instanceof NullPointerException){ System.out.println("java.lang.NullPointerException"); mv.setViewName("test"); } mv.addObject("error",e.toString()); return mv; } }
2) methods for exceptions in the Controller
@RequestMapping("/error") public String error() { String str=null; str.length(); return "test"; }
3) when the specified error occurs, return the specified View html
test.html view
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> //Error <span th:text="${error}"></span> </body> </html>