springMVC异常和处理

HandlerExceptionResolver

@ExceptionHandler作用范围是在当前类

@ControllerAdvice 是全局异常捕获

  1. DispatcherServlet 默认装配的 HandlerExceptionResolver
  2. 主要处理 Handler 中用 @ExceptionHandler 注解定义的方法。
  3. @ExceptionHandler 注解定义的方法优先级问题:先精确查找,再找其他的
  4. ExceptionHandlerMethodResolver 内部若找不到@ExceptionHandler 注解的话,会找
    @ControllerAdvice 中的@ExceptionHandler 注解方法

HandlerExceptionResolver 代码片段

handler 类中

//  @ExceptionHandler({RuntimeException.class})
//  public ModelAndView handleArithmeticException2(Exception ex){
//      System.out.println("[出异常了]: " + ex);
//      ModelAndView mv = new ModelAndView("error");
//      mv.addObject("exception", ex);
//      return mv;
//  }

    /**
     * 1. 在 @ExceptionHandler 方法的入参中可以加入 Exception 类型的参数, 该参数即对应发生的异常对象
     * 2. @ExceptionHandler 方法的入参中不能传入 Map. 若希望把异常信息传导页面上, 需要使用 ModelAndView 作为返回值
     * 3. @ExceptionHandler 方法标记的异常有优先级的问题. 
     * 4. @ControllerAdvice: 如果在当前 Handler 中找不到 @ExceptionHandler 方法来出来当前方法出现的异常, 
     * 则将去 @ControllerAdvice 标记的类中查找 @ExceptionHandler 标记的方法来处理异常. 
     */
//  @ExceptionHandler({ArithmeticException.class})
//  public ModelAndView handleArithmeticException(Exception ex){
//      System.out.println("出异常了: " + ex);
//      ModelAndView mv = new ModelAndView("error");
//      mv.addObject("exception", ex);
//      return mv;
//  }

@ControllerAdvice 修饰的类中

@ControllerAdvice
public class SpringMVCTestExceptionHandler {

    @ExceptionHandler({ArithmeticException.class})
    public ModelAndView handleArithmeticException(Exception ex){
        System.out.println("----> 出异常了: " + ex);
        ModelAndView mv = new ModelAndView("error");
        mv.addObject("exception", ex);
        return mv;
    }

}

ResponseStatusExceptionResolver 捕获异常

  1. 两种方式修饰 一种是方法上 另一种是 修饰一个异常类 在异常及异常父类中找到 @ResponseStatus 注解,然后使用这个注解的属性进行处理。
  2. 区别在于,只有修饰类是,方法跑出异常,过程不会走完.而修饰方法是,无论如何都会跑出异常,且走完方法

相关代码

修饰类

//value=HttpStatus.FORBIDDEN 指定错误编号| reason 指定显示内容
@ResponseStatus(value=HttpStatus.FORBIDDEN, reason="用户名和密码不匹配!")
public class UserNameNotMatchPasswordException extends RuntimeException{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


}

修饰方法

//都会跑出异常 |且当前方法走完
@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";
    }

DefaultHandlerExceptionResolver 默认处理器

对一些特殊的异常进行处理,比
如NoSuchRequestHandlingMethodException、HttpReques
tMethodNotSupportedException、HttpMediaTypeNotSuppo
rtedException、HttpMediaTypeNotAcceptableException
等。

SimpleMappingExceptionResolver

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

<!-- 配置使用 SimpleMappingExceptionResolver 来映射异常 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionAttribute" value="ex"></property>
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
            </props>
        </property>
    </bean> 
发布了30 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/wsl9420/article/details/52895437