全局异常处理机制与拦截器

全局异常处理机制

/**
 * 全局异常处理器
 * 应用到所有@RequestMapping注解的方法,在其抛出Exception异常时执行
 */
//@ControllerAdvice  //可以返回逻辑视图 转发和重定向的!
@RestControllerAdvice //直接返回json字符串
public class GlobalException {

    /*发生异常 -》 查看异常类型 @ExceptionHandler(指定的异常) -> 对应异常处理方法 handler
      指定的异常 可以精准查找,或者查找父异常!*/

    @ExceptionHandler(NullPointerException.class) //指定异常处理方法
    public Object nullPoing(NullPointerException e){
        String message = e.getMessage();
        return message;
    }
    @ExceptionHandler(Exception.class) //指定异常处理方法
    public Object allException(Exception e){
        String message = e.getMessage();
        return message;
    }
}
实例
@RestController
@RequestMapping("error")
public class TestException {

    @RequestMapping("e1")
    public String nullException(){
    String g=null;
    return g.toString();
    }

    @RequestMapping("e2")
    public Object exception(){
        int i=5/0;
        return i;
    }
}

拦截器

//springmvc 拦截器  实现接口HandlerInterceptor  自定义处理
public class MyInterceptor implements HandlerInterceptor {

    /**
     * 执行handler之前!调用的拦截方法!  编码格式设置。登录保护。权限处理!!
     * @param request 请求
     * @param response 响应
     * @param handler 要调用的方法对象
     * @return  true 放行  false拦截
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor.preHandle");
        return true;
    }

    /** 当handler执行完毕后。触发的方法! 没有拦截机制
     *  此方法只有 preHandler return true 触发
     * 对结果处理! 敏感词汇检查!!
     * @param request 请求
     * @param response 响应
     * @param handler 要执行的方法
     * @param modelAndView 返回的视图和共享域数据对象
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor.postHandle");
    }


    /**整体处理完毕 执行
     *
     * @param request
     * @param response
     * @param handler
     * @param ex  handler方法 报错 的异常对象
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor.afterCompletion");
    }
}

体验

@RestController
@RequestMapping("test")
public class InterceptorTest {

    @RequestMapping("e1")
    public String nullException(){
        System.out.println("InterceptorTest.nullException");
        return "e1";
    }

    @RequestMapping("e2")
    public int exception(){
        System.out.println("InterceptorTest.exception");
        return 1;
    }
    @RequestMapping("e3")
    public void test(){
        System.out.println("InterceptorTest.test");
    }
}
posted @   微风抚秀发  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示