SpringMVC通过HandlerExceptionResolver
处理程序的异常,包括Handler映射、数据绑定,以及目标方法执行时发生的异常。
SpringMVC默认使用下面三个ExceptionResolver:
HandlerExceptionResolver=
AnnotationMethodHandlerExceptionResolver,
ResponseStatusExceptionResolver,
DefaultHandlerExceptionResolver
但是自从SpringMVC中配置了
以后,AnnotationMethodHandlerExceptionResolver
被替换为ExceptionHandlerExceptionResolver
。所以SpringMVC中默认的就变成了下面三个异常处理器:
@ExceptionHandler
注解@ResponseStatus
注解页面渲染之前,有异常会先渲染异常
tomcat
。源码:
public class DispatcherServlet extends FrameworkServlet {
...
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
HandlerExecutionChain mappedHandler, ModelAndView mv, Exception exception) throws Exception {
boolean errorView = false;
【如果有异常】if (exception != null) {
if (exception instanceof ModelAndViewDefiningException) {
logger.debug("ModelAndViewDefiningException encountered", exception);
mv = ((ModelAndViewDefiningException) exception).getModelAndView();
}
else {
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
【处理异常】 mv = processHandlerException(request, response, handler, exception);//如果三个异常解析器都无法处理,会向上抛给tomcat。
errorView = (mv != null);
}
}
// Did the handler return a view to render?
if (mv != null && !mv.wasCleared()) {
【页面渲染】:render(mv, request, response);//来到页面
if (errorView) {
WebUtils.clearErrorRequestAttributes(request);
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName() +
"': assuming HandlerAdapter completed request handling");
}
}
if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
// Concurrent handling started during a forward
return;
}
if (mappedHandler != null) {
mappedHandler.triggerAfterCompletion(request, response, null);
}
}
...
}
@ExceptionHandler
标注在方法上,告诉SpringMVC这个方法专门处理这个类发生的异常。
不能给参数位置写Model
,正确的做法是返回ModelAndView。精确优先
。 @ExceptionHandler(value = { ArithmeticException.class, NullPointerException.class }) // 告诉SpringMVC,这个方法专门处理这个类发送的所有异常
public ModelAndView handleException01(Exception exception) {
System.out.println("handleException01..." + exception);
ModelAndView view = new ModelAndView("myerror");
view.addObject("ex", exception);
return view;
}
前端页面直接接受隐含模型
myerror.jsp页面
<body>
<h1>出错啦!h1>
<h2>出错信息:${ex }h2>
body>
统一异常管理
上面的测试是将@ExceptionHandler
放在了处理器中,实际上更好的方式是将@ExceptionHandler
放在一个单独的类中,进行全局异常处理
。
@ControllerAdvice
注解加入IoC容器中。本类优先
。@ControllerAdvice
public class MyException {
// 处理空指针异常
@ExceptionHandler(value = { NullPointerException.class })
public ModelAndView handleException01(Exception exception) {
System.out.println("全局的handleException01..." + exception);
ModelAndView view = new ModelAndView("myerror");
view.addObject("ex", exception);
return view;
}
// 处理算数异常
@ExceptionHandler(value = { ArithmeticException.class })
public ModelAndView handleException02(Exception exception) {
System.out.println("全局的handleException02..." + exception);
ModelAndView view = new ModelAndView("myerror");
view.addObject("ex", exception);
return view;
}
}
@ResponseStatus
标注在自定义异常上.
@ResponseStatus(reason = "用户被拒绝登录", value = HttpStatus.NOT_ACCEPTABLE)
public class UsernameNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
处理器中,抛出自定义UsernameNotFoundException
异常。
@RequestMapping("/handle02")
public String handle02(@RequestParam("username") String username) {
if (!"admin".equals(username)) {
System.out.println("登录失败");
throw new UsernameNotFoundException();
}
System.out.println("登录成功");
return "success";
}
发送username=admin02
的请求:
<a href="${ctp }/handle02?username=admin02">handl01a>
DefaultHandlerExceptionResolver判断是否是SpringMVC自带的异常。
例如定义如下的POST处理请求,通过GET进行访问,会出错。
@RequestMapping(value = "/handle03", method = RequestMethod.POST)
public String handle03() {
return "success";
}
SpringMVC自己的异常,如HttpRequestMethodNotSupportedException
。若没有处理,会进入到服务器错误页面。
默认的异常
源码:
public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
...
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
try {
if (ex instanceof NoSuchRequestHandlingMethodException) {
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, request, response,
handler);
}
else if (ex instanceof HttpRequestMethodNotSupportedException) {
return handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException) ex, request,
response, handler);
}
else if (ex instanceof HttpMediaTypeNotSupportedException) {
return handleHttpMediaTypeNotSupported((HttpMediaTypeNotSupportedException) ex, request, response,
handler);
}
else if (ex instanceof HttpMediaTypeNotAcceptableException) {
return handleHttpMediaTypeNotAcceptable((HttpMediaTypeNotAcceptableException) ex, request, response,
handler);
}
else if (ex instanceof MissingPathVariableException) {
return handleMissingPathVariable((MissingPathVariableException) ex, request,
response, handler);
}
else if (ex instanceof MissingServletRequestParameterException) {
return handleMissingServletRequestParameter((MissingServletRequestParameterException) ex, request,
response, handler);
}
else if (ex instanceof ServletRequestBindingException) {
return handleServletRequestBindingException((ServletRequestBindingException) ex, request, response,
handler);
}
else if (ex instanceof ConversionNotSupportedException) {
return handleConversionNotSupported((ConversionNotSupportedException) ex, request, response, handler);
}
else if (ex instanceof TypeMismatchException) {
return handleTypeMismatch((TypeMismatchException) ex, request, response, handler);
}
else if (ex instanceof HttpMessageNotReadableException) {
return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, request, response, handler);
}
else if (ex instanceof HttpMessageNotWritableException) {
return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler);
}
else if (ex instanceof MethodArgumentNotValidException) {
return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response,
handler);
}
else if (ex instanceof MissingServletRequestPartException) {
return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request,
response, handler);
}
else if (ex instanceof BindException) {
return handleBindException((BindException) ex, request, response, handler);
}
else if (ex instanceof NoHandlerFoundException) {
return handleNoHandlerFoundException((NoHandlerFoundException) ex, request, response, handler);
}
}
catch (Exception handlerException) {
if (logger.isWarnEnabled()) {
logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
}
}
return null;
}
...
}
SimpleMappingExceptionResolver
:通过配置的方式完成异常处理。
exceptionMappings属性
:配置不同的异常去不同的页面。key
:异常全类名。value
:表示要去的视图页面。exceptionAttribute属性
:指定错误信息取出时使用的key,默认为exception;可以通过这个key在前端获得异常信息。 <bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.NullPointerException">myerrorprop>
props>
property>
<property name="exceptionAttribute" value="ex">property>
bean>
前端显示页面
myerror.jsp
<body>
<h1>出错啦!h1>
<h2>出错信息:${ex }h2>
body>