spring 全局 异常 捕捉

其他技术

2018-01-02

34

0

技术:spring 全局异常

运行环境:IDEA 15.2 + jdk8 + windows 7 + spring-5.0.0.RELEASE

demo功能:提供一个spring全局异常捕捉的

全局异常捕捉处理

public class HandlerExceptionToViewResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
        System.out.println("=============================HandlerExceptionToViewResolver====================================================");
        System.out.println(e.toString());
        try {
            response.getWriter().write("{\"from\":\"HandlerExceptionToViewResolver\"}");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return null;
    }
}

注册异常处理器

spring 配置文件中添加

 

测试访问

运行项目,访问localhost:8080/index

更多

入口: org.springframework.web.servlet.doDispatch->processDispatchResult()->processHandlerException()

初始化:org.springframework.web.servlet.onRefresh->initStrategies()->initHandlerExceptionResolvers()->beansOfTypeIncludingAncestors()

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think