Spring Boot uses AOP to uniformly handle web layer exceptions.

Source: Internet
Author: User

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,springbootThesrc/main/resourcesFind 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.