- 在开发中,异常很常见的,我们需要捕捉异常并处理它,才能保证程序不被终止。最常见的异常处理方法就是用try catch来捕捉异常。
- Spring MVC通过HandlerExceptionResolver处理程序的异常,包括 Handler 映射、数据绑定以及目标方法执行时发生的异常。
SpringMVC 提供的 HandlerExceptionResolver 的实现类
DispatcherServlet 默认装配的 HandlerExceptionResolver :
如果添加了<mvc:annotation-driven/>
则变为:
1、ExceptionHandlerExceptionResolver
- ExceptionHandlerExceptionResolver主要是解析Handler中用 @ExceptionHandler注解定义的方法。
定义一个处理方法,处理过程中可能会存在抛出异常的情况:
@RequestMapping("testExceptionHandle")
public String testExceptionHandle(@RequestParam("i")Integer i){
System.out.println(10/i);
return "success";
}
当传递过来的 i = 0 时会产生数学异常
1.1 局部异常处理
在处理方法同一个类中定义异常处理方法:
//注意,该注解不是加在产生异常的方法上,而是加在处理异常的方法上
@ExceptionHandler({ArithmeticException.class})
public String testArithmeticException(Exception e){
System.out.println("打印错误信息 ===> ArithmeticException:"+e);
//跳转到指定页面
return "error";
}
- 当访问 http://localhost:8080/xxx/testExceptionHandle?i=0
时就会产生数学异常,被前置处理器捕获到,然后交给ExceptionHandlerExceptionResolver进行解析并分派处理,这里就交给了上面定义的异常处理方法testArithmeticException处理。 - @ExceptionHandler 的value属性是一个Class 数组,因此我们可以在该注解的属性中加入多个异常类.
- 当目标方法产生异常时,将被这个方法捕捉,我们可以得到异常的实例这个异常处理只能处理当前类产生的异常,是局部的异常处理方法。
- @ExceptionHandler注解定义的方法优先级问题:例如发生的是NullPointerException,但是声明的异常有RuntimeException和Exception,这时候会根据异常的最近继承关系找到继承深度最浅的那个@ExceptionHandler注解方法,即标记了RuntimeException 的方法
1.2 全局异常处理
新建一个处理异常的类,加上@ControllerAdvice注解
//标记当前类是用来处理异常的
@ControllerAdvice
public class HandleForException {
@ExceptionHandler({ArithmeticException.class})
public String testArithmeticException(Exception e){
System.out.println("打印错误信息 ===> ArithmeticException:"+e);
//跳转到指定页面
return "error";
}
}
- 当一个类加上@ControllerAdvice注解,告诉ExceptionHandlerExceptionResolver这个类是用来处理异常的。而具体的处理异常的方法加上@ExceptionHandler注解就行了。
- ExceptionHandlerMethodResolver先在处理类内部找被@ExceptionHandler
注解的方法,如果找不到,就会去被@ControllerAdvice注解的的类中的 @ExceptionHandler 注解方法
注意:当被 @ExceptionHandler标记为异常处理方法,不能在方法中设置别的形参。但是可以使用ModelAndView向前台传递数据。
2、ResponseStatusExceptionResolver
自定义一个异常类,使用@ResponseStatus 注解标注。若在处理器方法中抛出了这个异常,由于触发的异常带有@ResponseStatus注解。因此会被ResponseStatusExceptionResolver解析到。最后响应HttpStatus.xx状态码和reason信息给客户端。关于其他的响应码请参考 HttpStatus 枚举类型源码。
具体的使用方法如下:
2.1 首先定义一个异常类:
@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户名和密码不匹配!")
public class UserNameNotMatchPasswordException extends RuntimeException{
}
2.2 人为抛出上述异常
@RequestMapping("/testResponseStatusExceptionResolver")
public String testResponseStatusExceptionResolver(@RequestParam("i") int i){
if (i==13){
throw new UserNameNotMatchPasswordException();
}
System.out.println("testResponseStatusExceptionResolver....");
return "success";
}
访问http://localhost:8080/xxx/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、DefaultHandlerExceptionResolver
对一些特殊的异常进行处理,比如NoSuchRequestHandlingMethodException
、HttpRequestMethodNotSupportedException
、HttpMediaTypeNotSupportedException
、HttpMediaTypeNotAcceptableException
等。
会使用DefaultHandlerExceptionResolver进行解析,当发生这些异常时会返回给客户端一个处理过的页面,和ResponseStatusExceptionResolver很相似,只不过这里是系统为我们定义好的异常类和异常处理。
比如 NoSuchRequestHandlingMethodException异常:
定义一个处理方法,处理的是post请求:
@RequestMapping(value="/handleNoSuchRequestHandlingMethod",method=RequestMethod.POST)
public String handleNoSuchRequestHandlingMethod(){
System.out.println("handleNoSuchRequestHandlingMethod....");
return "success";
}
而客户端发送的是get请求:
<a href="handleNoSuchRequestHandlingMethod">handleNoSuchRequestHandlingMethod</a>
这时候就会触发NoSuchRequestHandlingMethodException异常,返回给客户端一个就能过处理的页面:
4、SimpleMappingExceptionResolver
如果希望对所有异常进行统一处理,可以使用SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告异常
在springmvc中配置
<!-- 配置使用 SimpleMappingExceptionResolver 来映射异常 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--
配置异常的属性值为ex,那么在错误页面中可以通过 ${ex} 来获取异常的信息
如果不配置这个属性,它的默认值为exception
-->
<property name="exceptionAttribute" value="ex"></property>
<property name="exceptionMappings">
<props>
<!-- 映射ArrayIndexOutOfBoundsException异常对应error.jsp这个页面 -->
<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
</props>
</property>
</bean>
处理方法
@RequestMapping("/testExceptionMapping")
public String testExceptionMapping(){
int arrays[] = new int[10];
System.out.println(arrays[11]);
return "hello";
}
很明显,访问这个处理方法是会产生数组下标越界异常,而上面配置了发生这个异常由error页面处理,并且将异常信息通过 ex 进行传递,所以在error.jsp页面:
<h2>${ex}</h2>
可以获取到异常信息