Springmvc 异常处理

在开始配置之前,我们先看一下,默认的异常处理视图和自定义的异常视图

 

下面,来看看如何配置统一异常处理

配置SpringMVC统一的异常处理非常简单,只需要在配置文件中加入如下配置即可

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
		<property name="exceptionMappings">
		<!-- 配置异常的映射关系 : 出现对应异常,跳转对应的页面 -->
			<props>
				<!-- 出现空指针异常,去error1逻辑视图 -->
				<prop key="java.lang.NullPointerException">error1</prop>
    			<!-- 出现IO异常,去error2逻辑视图 -->
				<prop key="java.io.FileNotFoundException">error2</prop>
			</props>
		</property>
		<!-- 将异常对象放入请求域中,在页面中通过exce这个key获取异常对象信息 -->
		<property name="exceptionAttribute" value="exce" />
</bean>

当然,如果你不像写配置文件,也可以通过注解来实现统一异常处理

//将当前类作为异常处理组件
@ControllerAdvice
public class MyException {
	
	//指定可以处理的异常
	@ExceptionHandler({NullPointerException.class,FileNotFoundException.class})
	public String dealException(Exception ex,Model model) {
		
		//将异常信息加入到模型中,在页面使用EL表达式获取即可
		model.addAttribute("exce", ex);
		
		//返回逻辑视图
		return "error1";
	}

}

总结 : SpringMVC配置统一异常处理有两种方式 :

  1. 使用xml配置 :org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
  2. 使用注解 :@ControllerAdvice,@ExceptionHandler

测试

随便在一个controller中整一个空指针异常,就可以看到,自定义异常配置生效

 讲解

通过配置文件中的全限定类名称可以看到,这个异常处理类是springmvc自带的异常处理类,所以只需要配置必要的属性和值就可以直接使用了。

当然,这个虽然是自带的异常处理类,但是却并不是默认的异常处理类。默认的异常处理类是

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver,

它里边的doResolveException方法就是处理异常的方法,然后返回视图对象。

同学们没有自己配置异常处理类的时候,浏览器界面上展示的异常信息就是这里给返回的视图

大家可以看到,里边有很多种类型的异常处理

其中我们常见的就是下面的这几个异常。

HttpRequestMethodNotSupportedException

NoSuchRequestHandlingMethodException

HttpMediaTypeNotSupportedException

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;
	}

既然有了默认的异常处理类,那么为什么还要自己配置异常处理呢?

因为默认的异常类对于普通用户而言提示并不友好,所以,我们需要自定义异常统一处理,让不同的异常,展示不同的界面,正如文章一开始的两个截图,自定义的异常视图,可以给与用户友好的体验。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的王小卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值