ExceptionHandler注解
示例:使用@ExceptionHandler注解处理异常
- 通过@ExceptionHandler注解标注一个方法,在这个方法所在类中若发生异常,则会执行@ExceptionHandler注解所标注的方法进行处理.
- 在处理异常的时候,可以使用ModelAndView对象,将异常信息带到页面之中,页面中可以使用EL表达式进行获取.
@ExceptionHandler(value = Exception.class)
public ModelAndView testExceptionHandler(Exception ex){
ModelAndView modelAndView = new ModelAndView();
//将异常信息保存到ModelAndView之中,这样可以暴露给网页中进行显示
modelAndView.addObject("exception",ex);
modelAndView.setViewName("error");
return modelAndView;
}
- 测试执行的请求方法
- 该方法进行一个除法运算,如果被除数为0,则会发生数学异常
@RequestMapping("testAdivision")
public String testAdivision(@RequestParam("y")Integer y, Map<String,Object> map){
map.put("result",7895/y);
return "success";
}
- 定义error.jsp页面
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>页面出错了O__O "…${exception}</h1>
</body>
</html>
使用@ControllerAdvice修饰类
-
当请求方法出现异常的时候,首先会在方法所在类中寻找@ExceptionHandler注解修饰的方法,倘若本类中没有@ExceptionHandler修饰的方法则会寻找@ControllerAdvice注解修饰的类中的@ExceptionAdvice方法
-
示例:@ControllerAdvice修饰类
@ControllerAdvice
public class ExceptionTool {
@ExceptionHandler(value = Exception.class)
public ModelAndView exceptionToolExceptionHandler(Exception ex) {
ModelAndView modelAndView = new ModelAndView();
//将异常信息保存到ModelAndView之中,这样可以暴露给网页中进行显示
modelAndView.addObject("exception", ex+"[ExceptionTool#exceptionToolExceptionHandler]");
modelAndView.setViewName("error");
return modelAndView;
}
}
ResponseStrutsExceptionResolver
- 当控制器抛出的异常没有被 @ExceptionHandler 注解的方法所捕获的时候,异常的信息会自动打印到客户端中.
- 异常页面包括状态码(Status)和提示信息.我们可以自定义错误页面的状态码和提示信息
- 当我们希望在返回的错误页面中,显示错误的状态码,和自定义的错误提示信息时,就可以使用到@ResponseStatus注解,该注解会被ResponseStrutsExceptionResolver类所解析,并将错误代码和错误提示信息返回给客户端
- ResponseStrutsExceptionResolver类中的resolveResponseStatus()方法
示例:自定义异常类,显示错误代码,和提示信息
- 定义一个UserPasswordException异常类
- 使用@ResponseStatus抛出403状态码,提示信息为:“错误访问”
@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "错误的访问")
public class UserPasswordException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
- 定义一个请求映射方法
public String testResponseStatus(@RequestParam("ps")Integer ps){
//如果请求参数=20,抛出UserPasswordException 异常
if(ps==20){
throw new UserPasswordException();
}
return "success";
}
- 测试请求路径
当@ResponseStatus注解修饰目标方法时
- 当使用@ResponseStatus修饰目标方法的时候,无论目标方法有没有出现异常都会显示错误页面,但是目标方法会被正常执行
@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "错误的访问")
@RequestMapping("/testResponseStatus")
public String testResponseStatus(@RequestParam("ps")Integer ps){
//如果请求参数=20,抛出UserPasswordException 异常
if(ps==20){
throw new UserPasswordException();
}
return "success";
}
DefaultHandlerExceptionResolver
-
DefaultHandlerExceptionResolver类中的部分代码
-
当处理器出现一些特殊的异常时,DefaultHandlerExceptionResolver类会将这些异常转换为特定的提示信息和状态码返回到客户端
-
NoSuchRequestHandlingMethod异常的转换方法
-
测试方法:
@RequestMapping("/testDefaultHandlerException")
public String testDefaultHandlerException() throws HttpRequestMethodNotSupportedException {
throw new HttpRequestMethodNotSupportedException("错误");
}
SimpleMappingExceptionResolver
- 示例:在SpringMVC配置文件中配置SimpleMappingExceptionResolver
- <property name=“exceptionAttribute” value=“ex”/>标签可以设置保存异常信息的属性名称
- <props>和<prop>标签定义多个异常映射路径
<!--配置异常映射路径转换器-->
<bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--
当出现异常后,simpleMappingExceptionResolver类会将异常信息保存到 request属性之中
默认的属性名称为"exception"
通过<property name="exceptionAttribute" value="exception"/>标签的配置可以定义属性名称
-->
<property name="exceptionAttribute" value="ex"/>
<!--配置异常映射路径-->
<property name="exceptionMappings">
<props>
<!--key为异常的全类名,当控制器出现对应的异常时,会跳转到对应的路径-->
<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
</props>
</property>
</bean>
- 错误页面
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>页面出错了O__O "…${ex}</h1>
</body>
</html>
- 测试方法:
@RequestMapping("/testSimpleMappingExceptionResolver")
public String testSimpleMappingExceptionResolver(@RequestParam("index")Integer index) {
int[] data = new int[10];
data[index] = (int) (Math.random()*10);
return "success";
}