Spring Boot uses AOP to uniformly handle web layer exceptions.
By default, springboot errors are redirected to the error/error page in the request's returned rendering path.
Source code analysis: DefaultErrorViewResolver. java
private ModelAndView resolve(String viewName, Map<String, Object> model) { String errorViewName = "error/" + viewName; TemplateAvailabilityProvider provider = this.templateAvailabilityProviders .getProvider(errorViewName, this.applicationContext); if (provider != null) { return new ModelAndView(errorViewName, model); } return resolveResource(errorViewName, model); }
For example, in application. properites, configure the rendering page
# Configure freemakerspring. freemarker. template-loader-path =/WEB-INF/
If not configuredspring.freemarker.template-loader-path,springboot
Thesrc/main/resources
Find the error rendering page under the error file in templates.
Then when an error occurs, the system will jump to the/WEB-INF/error page.
Use AOP for web layer Exception Handling
Package com. niugang. aop; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. http. httpServletRequest; import org. aspectj. lang. annotation. afterThrowing; import org. aspectj. lang. annotation. aspect; import org. slf4j. logger; import org. slf4j. loggerFactory; import org. springframework. stereotype. component; import org. springframework. web. context. request. requestAttributes; import org. springframework. web. context. request. requestContextHolder; import org. springframework. web. context. request. servletRequestAttributes; import org. springframework. web. servlet. modelAndView;/*** unified exception handling at the controller layer ** @ author niugang **/@ Aspect @ Componentpublic class ExceptionControllerAscept {private Logger logger = LoggerFactory. getLogger (ExceptionControllerAscept. class);/*** anonymous cut-off method ** @ param ex * @ throws ServletException * @ throws IOException */@ AfterThrowing (value = "execution (public * com. niugang. controller .. *. *(..)) ", throwing =" ex ") public ModelAndView aroundAdvice (Exception ex) throws ServletException, IOException {ModelAndView modelAndView = new ModelAndView (); RequestAttributes requestAttributes = RequestContextHolder. getRequestAttributes (); ServletRequestAttributes r = (ServletRequestAttributes) requestAttributes; HttpServletRequest request = r. getRequest (); modelAndView. setViewName ("500"); // first, if RuntimeException if (ex instanceof RuntimeException) {logger. error ("throwing a runtime exception {}", ex. getMessage (); modelAndView. addObject ("exception", ex. getMessage (); // jump to the error page modelAndView. addObject ("url", request. getRequestURL (); return modelAndView;} modelAndView. addObject ("exception", "unknown exception"); return modelAndView ;}}
Summary
The above section describes how to use AOP to handle web layer exceptions in Spring Boot. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!