6666666

4、调用确定的异常方法进行异常处理【跟正常的springmvc流程类似】
4.1、参数解析器列表、返回值处理器列表
4.2、调用真实的异常处理方法
4.3、进行返回值的处理
4.4、最后因为这个统一异常处理的方法标注了@RestControllerAdvice注解,所以通过ResponseBody的返回值处理器,通过httpMessageConvertor返回给前端的就是JSON字符串

//```java
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception exception) {

ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
if (exceptionHandlerMethod == null) {
	return null;
}

if (this.argumentResolvers != null) {
	exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
}
if (this.returnValueHandlers != null) {
	exceptionHandlerMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
}

ServletWebRequest webRequest = new ServletWebRequest(request, response);
ModelAndViewContainer mavContainer = new ModelAndViewContainer();

try {
	if (logger.isDebugEnabled()) {
		logger.debug("Using @ExceptionHandler " + exceptionHandlerMethod);
	}
	Throwable cause = exception.getCause();
	if (cause != null) {
		// Expose cause as provided argument as well
		exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod);
	}
	else {
		// Otherwise, just the given exception as-is
		exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
	}
}
catch (Throwable invocationEx) {
	// Any other than the original exception is unintended here,
	// probably an accident (e.g. failed assertion or the like).
	if (invocationEx != exception && logger.isWarnEnabled()) {
		logger.warn("Failure in @ExceptionHandler " + exceptionHandlerMethod, invocationEx);
	}
	// Continue with default processing of the original exception...
	return null;
}

if (mavContainer.isRequestHandled()) {
	return new ModelAndView();
}
else {
	ModelMap model = mavContainer.getModel();
	HttpStatus status = mavContainer.getStatus();
	ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, status);
	mav.setViewName(mavContainer.getViewName());
	if (!mavContainer.isViewReference()) {
		mav.setView((View) mavContainer.getView());
	}
	if (model instanceof RedirectAttributes) {
		Map<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
		RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
	}
	return mav;
}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Fire Fish

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

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

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

打赏作者

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

抵扣说明:

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

余额充值