博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC_异常
阅读量:3756 次
发布时间:2019-05-22

本文共 3252 字,大约阅读时间需要 10 分钟。

一、异常

1、异常处理

  • SpringMVC 通过 HandlerExceptionResolver 处理程序的异常,包括 Handler 映射、数据绑定以及目标方法执行时发生的异常。
  • SpringMVC 提供的 HandlerExceptionResolver 的实现类

2、HandlerExceptionResolver 

         2)使用了 <mvc:annotation-driven/>配置:

  • DispatcherServlet 默认装配的 HandlerExceptionResolver:
    1)没有使用 <mvc:annotation-driven/> 配置:

       2)使用了 <mvc:annotation-driven/> 配置:

           

3、ExceptionHandlerExceptionResolver

  •  主要处理Handler中用 @ExceptionHandler 注解定义的方法。
  • @ExceptionHandler 注解定义方法的优先级问题:例如发生的是NullPointerException,但是声明的异常有 RuntimeException 和 Exception,此时会根据异常最近的继承关系找到继承深度最浅的那个 @ExceptionHandler 注解方法,即标记了RuntimeException的方法。
    @RequestMapping(value = "/testExceptionHandlerExceptionResolver")    public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){        System.out.println("result:"+(10/i));        return "success";    }    /**     * 1、在 @ExceptionHandler 方法的入参中可以加入 Exception 类型的参数,该参数即对应发生的异常对象     * 2、 @ExceptionHandler 方法的入参中不能传入Map ,若希望把异常信息传到页面上,需要使用 ModelAndView作为返回值     * 3、@ExceptionHandler  方法标记的异常有优先级的问题     * 4、@ControllerAdvice 如果在当前 Handler中找不到 @ ExceptionHandler 方法来处理当前方法的异常,将去     *   @ControllerAdvice标记的类中查找来处理异常     * @param ex     * @return     */    @ExceptionHandler({ArithmeticException.class})    public ModelAndView handlerArithmeticExceprion(Exception ex){        System.out.println("出异常了:"+ex);        ModelAndView mv = new ModelAndView("error");        mv.addObject("ex",ex);        return mv;    }

     

  • @ExceptionHandlerMethodResolver 内部若找不到 @ExceptionHandler 注解的话,会找 @ControllerAdvice 中的 @ExceptionHandler 注解方法。
    @ControllerAdvicepublic class Exception {    @ExceptionHandler({ArithmeticException.class})    public ModelAndView handlerArithmeticExceprion(java.lang.Exception ex){        System.out.println("出异常了!!!:"+ex);        ModelAndView mv = new ModelAndView("error");        mv.addObject("ex",ex);        return mv;    }}

     

4、ResponseStatusExceptionResolver

  • 在异常及父类异常中找到 @ResponseStatus 注解,然后使用这个注解的属性进行处理。
  • 定义一个 @ResponseStatus 注解修饰的异常类
  • 若在处理器方法上抛出了上述异常:

    若 ExceptionHandlerExceptionResolver 不解析上述异常。由于触发的异常 UnauthorizedException 带有 @ResponseStatus注解。因此会被 ResponseStatusExceptionResolver解析到。最后影响 HttpStatus.UNAUTHORIZED代码给客户端。HttpStatus.UNAUTHORIZED代表响应码401,无权限。关于其他的响应码参考 HttpStatus枚举类型源码。

@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "不匹配")public class NotMatchException extends RuntimeException {}
@RequestMapping(value = "/responseStatusExceptionResolver")    public String responseStatusExceptionResolver(@RequestParam("i") int i){        if (i==13){            throw new NotMatchException();        }        System.out.println("responseStatusExceptionResolver");        return "success";    }

5、DefaultHandlerExceptionResolver

对一些特殊的异常进行处理,比如NoSuchRequestHandlingMethodException、HttpRequestMethodNotSupportedException、HttpMediaTypeNotSupportedException、HttpMediaTypeNotAcceptableException等。

 

6、SimpleMappingExceptionResolver

如果希望对所有的异常进行统一的处理,可以使用SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告。

@RequestMapping(value = "SimpleMappingExceptionResolver")    public String SimpleMappingExceptionResolver(@RequestParam("i") int i){        String[] vals = new String [10];        System.out.println(vals[i]);        return "success";    }
error

 

你可能感兴趣的文章
练习——图书管理系统八(根据图书编号填充图书名称下拉控件和验证手机号)
查看>>
将windows下文件上传至服务器中
查看>>
正则表达式:贪婪模式与懒惰模式
查看>>
机器学习之sklearn.preprocessing.LabelBinarizer()的用法
查看>>
决策树剪枝的思想
查看>>
创建二叉树和遍历二叉树
查看>>
算法训练 区间k大数查询
查看>>
算法训练 K好数
查看>>
2021北京交通大学计算机专硕初试经验分享
查看>>
2021北京交通大学计算机专硕复试经验分享
查看>>
简单实现一个数组、链表
查看>>
关于从其他程序切回word文档时卡顿问题的解决办法
查看>>
Linux安装Java环境
查看>>
Java集合 ArrayList原理
查看>>
Git的基本操作
查看>>
简述128陷阱
查看>>
在spring boot项目中修改包名要注意的一些问题
查看>>
编写类实现从后台向前台返回所要求的数据
查看>>
spring boot的学习(1.创建一个初始的spring boot项目)
查看>>
Python的入门学习
查看>>