Spring MVC 通过 HandlerExceptionResolver 处理程序的异常,包含 Handler 映射、数据绑定以及目标方法运行时发生的异常。

SpringMVC 提供的 HandlerExceptionResolver 的实现类


DispatcherServlet 默认装配的 HandlerExceptionResolver 


假设加入了<mvc:annotation-driven/> 则装配变为:




1.1. ExceptionHandlerExceptionResolver

ExceptionHandlerExceptionResolver 主要处理 Handler 中用 @ExceptionHandler 注解定义的方法。

@ExceptionHandler 注解定义的方法优先级问题:比如发生的是NullPointerException,可是声明的异常有RuntimeException 和 Exception,此候会依据异常的近期继承关系找到继承深度最浅的那个 @ExceptionHandler注解方法。即标记了 RuntimeException 的方法

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 代码给client。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 

对一些特殊的异常进行处理。比方NoSuchRequestHandlingMethodExceptionHttpRequestMethodNotSupportedExceptionHttpMediaTypeNotSupportedExceptionHttpMediaTypeNotAcceptableException等。


	@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>