Spring MVC 通过 HandlerExceptionResolver 处理程序的异常,包括 Handler 映射、数据绑定以及目标方法执行时发生的异常。
SpringMVC 提供的 HandlerExceptionResolver 的实现类
DispatcherServlet 默认装配的 HandlerExceptionResolver :
如果添加了<mvc:annotation-driven/> 则装配变为:
1.1. ExceptionHandlerExceptionResolver
ExceptionHandlerExceptionResolver 主要处理 Handler 中用 @ExceptionHandler 注解定义的方法。
l @ExceptionHandler 注解定义的方法优先级问题:例如发生的是NullPointerException,但是声明的异常有RuntimeException 和 Exception,此候会根据异常的最近继承关系找到继承深度最浅的那个 @ExceptionHandler注解方法,即标记了 RuntimeException 的方法
l ExceptionHandlerMethodResolver 内部若找不到 @ExceptionHandler 注解的话,会找 @ControllerAdvice 中的 @ExceptionHandler 注解方法
Java:
package com.ibigsea.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ExceptionController {
/**
* 如果执行此Controller里面的方法出现<b>数学异常</b>,则会执行该方法
* @param e
* @return
*/
@ExceptionHandler({ArithmeticException.class})
public String arithmeticException(Exception e){
System.out.println("arithmeticException:"+e);
return "error";
}
/**
* 如果执行此Controller里面的方法出现异常,则会执行该方法
* @param e
* @return
*/
@ExceptionHandler({Exception.class})
public String exceptionHandle(Exception e){
System.out.println("Exception"+e);
return "error";
}
@RequestMapping("/exception")
public String exception(Integer i){
System.out.println(10/i);
return "success";
}
}
上述异常处理代码会优先执行arithmeticException方法.
或者在类上面添加@ControllerAdvice注解
package com.ibigsea.springmvc.controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class TestExceptionHandler {
@ExceptionHandler({ArithmeticException.class})
public String handleException(Exception e) {
System.out.println("----> 出异常了: " + e);
return "error";
}
}
注意:
因为在执行@ExceptionHandler修饰的方法时没有传入BindingAwareModelMap对象,所以不能在方法上面添加额外的形参
1.2. ResponseStatusExceptionResolver
l 在异常及异常父类中找到 @ResponseStatus 注解,然后使用这个注解的属性进行处理。
l 定义一个 @ResponseStatus 注解修饰的异常类
l 若在处理器方法中抛出了上述异常:若ExceptionHandlerExceptionResolver 不解析该异常。由于触发的异常 UnauthorizedException 带有@ResponseStatus注解。因此会被ResponseStatusExceptionResolver 解析到。最后响应HttpStatus.UNAUTHORIZED 代码给客户端。HttpStatus.UNAUTHORIZED 代表响应码401,无权限。关于其他的响应码请参考 HttpStatus 枚举类型源码。
Java:
@RequestMapping("/responseStatus")
public String responseStatus(Integer i){
if (i>20) {
throw new TestResponseStatusException();
}
return "success";
}
package com.ibigsea.springmvc.exceptionresolver;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value=HttpStatus.FORBIDDEN, reason="测试@ResponseStatus!")
public class TestResponseStatusException extends RuntimeException {
private static final long serialVersionUID = 4566548902417978204L;
}
结果:
1.3. DefaultHandlerExceptionResolver
对一些特殊的异常进行处理,比如NoSuchRequestHandlingMethodException、HttpRequestMethodNotSupportedException、HttpMediaTypeNotSupportedException、HttpMediaTypeNotAcceptableException等。
@RequestMapping(value="/defaultHandler",method=RequestMethod.POST)
public String defaultHandlerException(){
return "success";
}
1.4. SimpleMappingExceptionResolver
如果希望对所有异常进行统一处理,可以使用SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告异常
JAVA
@RequestMapping("/simpleMapping")
public String simpleMappingException(Integer i){
String[] strs = new String[10];
System.out.println(strs[i]);
return "success";
}
Spring-mvc.xml
<!-- 配置使用SimpleMappingExceptionResolver来映射异常 -->
<bean id="simpleMappingExceptionResolver" 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>