Spring MVC 通过 HandlerExceptionResolver 处理程序的异常,
包括
Handler 映射、数据绑定以及目标方法执行时发生的异常。
• SpringMVC 提供的 HandlerExceptionResolver 的实现类
DispatcherServlet 默认装配的 HandlerExceptionResolver :
– 没有使用 <mvc:annotation-driven/> 配置:
– 使用了 <mvc:annotation-driven/> 配置:
ExceptionHandlerExceptionResolver
• 主要处理 Handler 中用 @ExceptionHandler 注解定义的方法。
• @ExceptionHandler 注解定义的方法优先级问题:例如发生的是NullPointerException,但是声明的异常有RuntimeException 和 Exception,此候会根据异常的最近继承关系找到继承深度最浅的那个 @ExceptionHandler注解方法,即标记了 RuntimeException 的方法
• ExceptionHandlerMethodResolver 内部若找不到@ExceptionHandler 注解的话,会找@ControllerAdvice 中的@ExceptionHandler 注解方法
@ExceptionHandler({ArithmeticException.class})
public String handleArithmeticException(Exception ex){
System.out.println("error : " + ex);
return "error";
}
@RequestMapping(value="/testExceptionHandlerExceptionResolver")
public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){
System.out.println("result: " + (12/i));
System.out.println();
return SUCCESS;
}
注意:
* 1. 在 @ExceptionHandler 方法的入参中可以加入 Exception 类型的参数, 该参数即对应发生的异常对象
* 2. @ExceptionHandler 方法的入参中不能传入 Map. 若希望把异常信息传导页面上, 需要使用 ModelAndView 作为返回值
@ExceptionHandler({ArithmeticException.class})
public ModelAndView handleArithmeticException(Exception ex){
System.out.println("error : " + ex);
ModelAndView mv = new ModelAndView( "error" );
mv.addObject("exception", ex);
return mv;
}
在页面上打印
${exception}
* 3. @ExceptionHandler 方法标记的异常有优先级的问题.
* 4. @ControllerAdvice: 如果在当前 Handler 中找不到 @ExceptionHandler 方法来出来当前方法出现的异常, 则将去 @ControllerAdvice 标记的类中查找 @ExceptionHandler 标记的方法来处理异常.
package springmvc;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
@ControllerAdvice
public class TestHandlerException {
@ExceptionHandler({ArithmeticException.class})
public ModelAndView handleArithmeticException(Exception ex){
System.out.println("***error : " + ex);
ModelAndView mv = new ModelAndView( "error" );
mv.addObject("exception", ex);
return mv;
}
}
ResponseStatusExceptionResolver
• 在异常及异常父类中找到 @ResponseStatus 注解,然后使用这个注解的属性进行处理。
• 定义一个 @ResponseStatus 注解修饰的异常类
• 若在处理器方法中抛出了上述异常:
若ExceptionHandlerExceptionResolver 不解析述异常。由于触发的异常 UnauthorizedException 带有@ResponseStatus注解。因此会被ResponseStatusExceptionResolver 解析到。最后响应HttpStatus.UNAUTHORIZED 代码给客户端。HttpStatus.UNAUTHORIZED 代表响应码401,无权限。
关于其他的响应码请参考 HttpStatus 枚举类型源码。
DefaultHandlerExceptionResolver
• 对一些特殊的异常进行处理,比如NoSuchRequestHandlingMethodException、HttpRequestMethodNotSupportedException、HttpMediaTypeNotSupportedException、HttpMediaTypeNotAcceptableException等。
SimpleMappingExceptionResolver
• 如果希望对所有异常进行统一处理,可以使用SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告异常