• SpringMVC中异常处理详解


    Spring MVC处理异常最基本的就是HandlerExceptionResolver这个接口,先看张图

    分析上图可以轻松总结出,spring mvc里有三种异常处理方法:

    1.使用官方提供的简单异常处理器SimpleMappingExceptionResolver

    使用示例

    springMVC-servlet.xml

    <beanclass="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
      定义默认的异常处理页面,当该异常类型的注册时使用
      <property name="defaultErrorView" value="error">
      </property>
      定义异常处理页面用来获取异常信息的变量名,默认名为exception
      <propertyname="exceptionAttribute"value="ex"></property>
      定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值
      <property name="exceptionMappings">
      <props>
      <propkey="etc.exception.MyException">error-my</prop>
      这里还可以继续扩展对不同异常类型的处理
      </props>
      </property>
     </bean> 

    MyExceptionHandler.java

    public class MyExceptionHandler{
        
        @ExceptionHandler
        public ModelAndView resolveException(HttpServletRequest request,
                HttpServletResponse response, Object handler, Exception ex) {
            Map<String, Exception> map = new HashMap<String, Exception>();
            map.put("ex",ex);
            
            // 根据获取的Exception参数进行view跳转
            if (ex instanceof MyException) {
                return new ModelAndView("error-my",map);
            } else {
                return new ModelAndView("error",map);
            }
        }
    }

    定义了这样一个异常处理器之后就要在springMVC-servlet.xml中定义这样一个bean对象,如

    <bean id="myExceptionHandler" class="com.tiantian.xxx.web.handler.myExceptionHandler"/>

    2.使用@ResponseStatus注解

    带有@ResponseStatus注解的异常类会被ResponseStatusExceptionResolver 解析。可以实现自定义的一些异常,同时在页面上进行显示。

    使用示例

    首先定义一个异常类:

    @ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户名和密码不匹配!")
    public class UserNameNotMatchPasswordException extends RuntimeException{
            
    }

    抛出一个异常:

      @RequestMapping("/testResponseStatusExceptionResolver")
        public String testResponseStatusExceptionResolver(@RequestParam("i") int i){
            if (i==13){
                throw new UserNameNotMatchPasswordException();
            }
            System.out.println("testResponseStatusExceptionResolver....");
            return "success";
        }

    输入如下额路径:

    http://localhost:8090/testResponseStatusExceptionResolver?i=13

    当然,也可以在方法上进行修饰:

    @ResponseStatus(reason = "测试",value = HttpStatus.NOT_FOUND)
        @RequestMapping("/testResponseStatusExceptionResolver")
        public String testResponseStatusExceptionResolver(@RequestParam("i") int i){
            if (i==13){
                throw new UserNameNotMatchPasswordException();
            }
            System.out.println("testResponseStatusExceptionResolver....");
            return "success";
        }

    这时所有的请求都会报错。

    3.使用全局异常处理

    @ExceptionHandler异常统一处理

  • 相关阅读:
    基于mybatis的crud demo
    事务的隔离级别
    spring中ioc的实现
    spring中xml配置文件
    spring中AOP的实现
    mybatis框架
    基于Mapreduce的并行Dijkstra算法执行过程分析
    算法技巧:位运算 逻辑运算
    day04_09 while循环03
    day04_08 while循环02
  • 原文地址:https://www.cnblogs.com/shamo89/p/9949136.html
Copyright © 2020-2023  润新知