Spring 框架——利用HandlerExceptionResolver实现全局异常捕获

一、需求描述

        因为在项目中,我们不可否认的会出现异常,而且这些异常并没有进行捕获。经常出现的bug如空指针异常等等。在之前的项目中,如果我们没有进行任何配置,那么容器会自动打印错误的信息,如果tomcat的404页面,400页面等等。如果我们在web.xml中进行如下配置,就会拦截错误,然后跳转到指定的错误页面。

<error-page>
    <error-code>500</error-code>
    <location>/500.jsp</location>
</error-page>

但是这已经落后了,现在我们通过实现spring的HandlerExceptionResolver接口来捕获所有的异常。

二、基本应用

        首先我们新建GlobalExceptionResolver如下:

public class GlobalExceptionResolver implements HandlerExceptionResolver{
​
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception exception) {
        //--------------------------------------------
        return null;
    }
}

然后在spring配置文件中配置刚才新建的类,或者加上@Component注解。

<!--全局异常捕捉 -->
<bean class="com.ssm.exception.GlobalExceptionResolver" />

现在就可以根据自己的需求修改GlobalExceptionResolver的横线部分了,在你在开发的时候,请返回null,这样这个类就如同不起作用,之前该怎么样就怎么样。当开发完成之后,根据错误的信息来返回需要的东西了。首先我们可以做的是打印错误日志,当然也是必须的。

System.err.println("访问" + request.getRequestURI() + " 发生错误, 错误信息:" + exception.getMessage());

这里我只是用控制台举例,你当然可以用日志框架打印。打印信息主要是包括是访问那个地址出现了什么错误。之后如果你需要返回错误页面,那么就直接在ModelAndView里面写就行了,这里就不多举例了,ModelAndView写过MVC的Controller应该都熟悉。

ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("error");
return modelAndView;

 以上其实就已经实现了全局捕获异常的功能,你可以自己抛出一个不捕获的异常测试一下是否成功。

三、其他说明

注意不同类型和来源的请求

        因为在实际项目中,可能遇到各种请求类型,如正常的get,post。也可能是来自ajax的请求。所以如果均返回同一个ModelAndView显然可能有点不合适,对于ajax可能需要特别处理。

        还有就是如果有手机端和PC在同一个项目中的情况,那么来源不同,返回的页面也可能不同。虽然可以交给前端去做自适应处理,但是我们还是得做好考虑。

        总之,要考虑到各种不同的请求,单一返回可能并不适用所有项目。

GlobalExceptionResolver这个类推荐放在exception包下,属于一种自定义异常

        这个配置推荐放在和web相关的spring配置下,因为和类似一个controller

spring是怎么做到的?handler参数又是什么?

        首先spring官方文档536页说明了HandlerExceptionResolve,而官方推荐的是使用@ExceptionHandler注解去捕获固定的异常。然后我查了源码,spring源码中:

/**
     * Resolve the exception by iterating over the list of configured exception resolvers.
     * The first one to return a ModelAndView instance wins. Otherwise {@code null} is returned.
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler,
                                         Exception ex) {
        if (resolvers != null) {
            for (HandlerExceptionResolver handlerExceptionResolver : resolvers) {
                ModelAndView mav = handlerExceptionResolver.resolveException(request, response, handler, ex);
                if (mav != null) {
                    return mav;
                }
            }
        }
        return null;
    }

这是spring默认实现,也就是说,我们没有重写的话,spring是这样执行的,从命名来瞎说就是。如果出现异常(private List<HandlerExceptionResolver> resolvers;),处理异常解析器就会非空,通过循环异常解析器处理resolvers中的异常,然后处理。最后返回null也就是我们之前所说的不做任何错误页面的那种处理。然后处理异常打印异常信息是在抽象类里面完成的:

/**
     * Check whether this resolver is supposed to apply (i.e. if the supplied handler
     * matches any of the configured {@linkplain #setMappedHandlers handlers} or
     * {@linkplain #setMappedHandlerClasses handler classes}), and then delegate
     * to the {@link #doResolveException} template method.
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
            Object handler, Exception ex) {
​
        if (shouldApplyTo(request, handler)) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Resolving exception from handler [" + handler + "]: " + ex);
            }
            prepareResponse(ex, response);
            ModelAndView result = doResolveException(request, response, handler, ex);
            if (result != null) {
                logException(ex, request);
            }
            return result;
        }
        else {
            return null;
        }
    }

就是打印错误信息,这里我们看到handler被打印了。打印的意思是从哪一个handler解析出什么异常。最后如果有结果依旧返回。总之我们可以知道的是,spring的handle在处理时发现异常后,HandlerExceptionResolver的列表会被赋值,然后进行处理。

四、项目运用

        这里我们可以参考《异常处理》一节阅读,首先我们的全局异常拦截器实现如下:

/**
 * 
 * 类名称 : SgccExceptionResolver. <br>
 * 功能描述 : 全局异常拦截器,可在此做异常信息的判断及输出. <br>
 * <p>
 * 创建人: Administrator <br>
 * 创建时间 : 2017年7月3日 下午15:12:36. <br>
 * </p>
 * 修改历史:  <br>
 * 修改人  修改日期   修改描述<br>
 * -----------------------------------<br>
 */
public class SgccExceptionResolver implements HandlerExceptionResolver {
​
    private Logger logger = Logger.getLogger(this.getClass());
​
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        logger.info("==============Exception Start 000000=============");
        if (ex instanceof BaseException) {
            logger.debug(ex, ex);
        }else {
            logger.error(ex, ex);
        }
        logger.info("==============Exception End 000000=============");
        if (NetworkUtil.isAjax(request)) {
            String msg = null;
            String code = null;
            String detail = null;
            if (ex instanceof BaseException) {
                msg = ((BaseException) ex).getErrorMsg();
                code = ((BaseException) ex).getErrorCode();
                detail = ((BaseException) ex).getMsgDetail();
            }else {
                FSTErrorCode fc = FSTErrorCode.SYS_ERROR_000000;
                msg = fc.getErrorMsg();
                code = fc.getErrorCode();
                detail = fc.getMsgDetail();
            }
            try {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("msg", msg);
                map.put("code", code);
                map.put("detail", detail);
                JSONObject json = JSONObject.fromObject(map);
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().print(json);
            }catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }else {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("error/error");
            mv.addObject("exception", ex.toString().replaceAll("\n", "<br/>"));
            return mv;
        }
    }    
    
}

猜你喜欢

转载自blog.csdn.net/qq_22172133/article/details/82147630