zoukankan      html  css  js  c++  java
  • 15.Spring-Boot中使用AOP统一处理web层异常

    在springboot错误默认是跳转到 请求返回渲染路径中的error/错误页面中。

    源码分析: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);
    }

    比如在application.properites中配置渲染页面为

    #配置freemaker

    spring.freemarker.template-loader-path=/WEB-INF/

    如果不配置spring.freemarker.template-loader-path,springboot会在src/main/resources中的templates中的error文件下下找错误渲染的页面。

    那么当出现错误时,系统会跳转到/WEB-INF/error/错误页面中。

    使用AOP进行web层异常处理

    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;
    
    
    /**
     * controller层统一异常处理
     * 
     * @author niugang
     *
     */
    @Aspect
    
    @Component
    
    public class ExceptionControllerAscept {
    private Logger logger = LoggerFactory.getLogger(ExceptionControllerAscept.class);
    /**
    * 匿名切点的方式
    * 
    * @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");
    // 第一如果是 RuntimeException
    if (ex instanceof RuntimeException) {
    logger.error("抛出运行时异常{}", ex.getMessage());
    modelAndView.addObject("exception", ex.getMessage());
    // 跳转到错误页面
    modelAndView.addObject("url", request.getRequestURL());
    return modelAndView;
    }
    modelAndView.addObject("exception","未知异常");
    return modelAndView;
    }
    }

           

    微信公众号

                              
  • 相关阅读:
    Linux命令学习—— fdisk -l 查看硬盘及分区信息
    UE4 Runtime下动态给Actor添加组件
    如何批量下载网站网页
    ue4 motage
    帧同步相关
    张瀚荣:如何用UE4制作3D动作游戏
    游戏服务器架构演进(完整版)
    Digital Mike头发制作及渲染的深度揭秘
    [UE4]如何替换角色Mesh上的Material材质
    [UE4]用C++如何创建Box Collision
  • 原文地址:https://www.cnblogs.com/niugang0920/p/12196734.html
Copyright © 2011-2022 走看看