2024年最新SpringMVC 异常处理体系深入分析,springcloud入门教程

读者福利

分享一份自己整理好的Java面试手册,还有一些面试题pdf

不要停下自己学习的脚步

字节跳动的面试分享,为了拿下这个offer鬼知道我经历了什么

字节跳动的面试分享,为了拿下这个offer鬼知道我经历了什么

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

2.1 AbstractHandlerMethodExceptionResolver

AbstractHandlerMethodExceptionResolver 主要是重写了 shouldApplyTo 方法和 doResolveException 方法,一个一个来看。

shouldApplyTo

@Override

protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) {

if (handler == null) {

return super.shouldApplyTo(request, null);

}

else if (handler instanceof HandlerMethod) {

HandlerMethod handlerMethod = (HandlerMethod) handler;

handler = handlerMethod.getBean();

return super.shouldApplyTo(request, handler);

}

else if (hasGlobalExceptionHandlers() && hasHandlerMappings()) {

return super.shouldApplyTo(request, handler);

}

else {

return false;

}

}

这块感觉没啥好说的,判断逻辑基本上都还是调用父类的 shouldApplyTo 方法去处理。

doResolveException

@Override

@Nullable

protected final ModelAndView doResolveException(

HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {

HandlerMethod handlerMethod = (handler instanceof HandlerMethod ? (HandlerMethod) handler : null);

return doResolveHandlerMethodException(request, response, handlerMethod, ex);

}

@Nullable

protected abstract ModelAndView doResolveHandlerMethodException(

HttpServletRequest request, HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception ex);

doResolveException 是具体的异常处理方法,但是它里边却没有实质性操作,具体的事情交给 doResolveHandlerMethodException 方法去做了,而该方法是一个抽象方法,具体的实现在子类中。

2.1.1 ExceptionHandlerExceptionResolver

AbstractHandlerMethodExceptionResolver 只有一个子类就是 ExceptionHandlerExceptionResolver,来看下它的 doResolveHandlerMethodException 方法:

@Override

@Nullable

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();

ArrayList exceptions = new ArrayList<>();

try {

if (logger.isDebugEnabled()) {

logger.debug("Using @ExceptionHandler " + exceptionHandlerMethod);

}

// Expose causes as provided arguments as well

Throwable exToExpose = exception;

while (exToExpose != null) {

exceptions.add(exToExpose);

Throwable cause = exToExpose.getCause();

exToExpose = (cause != exToExpose ? cause : null);

}

Object[] arguments = new Object[exceptions.size() + 1];

exceptions.toArray(arguments); // efficient arraycopy call in ArrayList

arguments[arguments.length - 1] = handlerMethod;

exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, arguments);

}

catch (Throwable invocationEx) {

// Any other than the original exception (or a cause) is unintended here,

// probably an accident (e.g. failed assertion or the like).

if (!exceptions.contains(invocationEx) && 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;

}

}

这个方法虽然比较长,但是很好理解:

  1. 首先查找到带有 @ExceptionHandler 注解的方法,封装成一个 ServletInvocableHandlerMethod 对象(关于 ServletInvocableHandlerMethod 对象,松哥在之前的文章中已经介绍过了,具体参见:Spring Boot 定义接口的方法是否可以声明为 private?)。

  2. 如果找到了对应的方法,则为 exceptionHandlerMethod 配置参数解析器、视图解析器等,关于这些解析器,参考松哥之前的文章:SpringBoot 中如何自定义参数解析器?深入分析 SpringMVC 参数解析器Spring Boot 中如何统一 API 接口响应格式?

  3. 接下来定义一个 exceptions 数组,如果发生的异常存在异常链,则将整个异常链存入 exceptions 数组中。

  4. exceptions 数组再加上 handlerMethod,共同组成方法参数,调用 exceptionHandlerMethod.invokeAndHandle 完成自定义异常方法的执行,执行结果被保存再 mavContainer 中。

  5. 如果请求到此结束,则直接构造一个 ModelAndView 返回。

  6. 否则从 mavContainer 中取出各项信息,构建新的 ModelAndView 返回。同时,如果存在重定向参数,也将之保存下来(关于重定向参数,参见:SpringMVC 中的参数还能这么传递?涨姿势了!)。

这就是 ExceptionHandlerExceptionResolver 的大致工作流程,可以看到,还是非常 easy 的。

2.2 DefaultHandlerExceptionResolver

这个看名字就知道是一个默认的异常处理器,用来处理一些常见的异常类型,我们来看一下它的 doResolveException 方法:

@Override

@Nullable

protected ModelAndView doResolveException(

HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {

try {

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

}

else if (ex instanceof AsyncRequestTimeoutException) {

return handleAsyncRequestTimeoutException(

(AsyncRequestTimeoutException) ex, request, response, handler);

}

}

catch (Exception handlerEx) {

}

return null;

}

可以看到,这里实际上就是根据不同的异常类型,然后调用不同的类去处理该异常。这里相关的处理都比较容易,以 HttpRequestMethodNotSupportedException 为例,异常处理就是对 response 对象做一些配置,如下:

protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,

HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {

String[] supportedMethods = ex.getSupportedMethods();

if (supportedMethods != null) {

response.setHeader(“Allow”, StringUtils.arrayToDelimitedString(supportedMethods, ", "));

}

response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());

return new ModelAndView();

}

配置响应头,然后 sendError,最后返回一个空的 ModelAndView 对象。

其实这里哥哥异常处理方法都大同小异,松哥就不再赘述啦。

2.3 ResponseStatusExceptionResolver

这个用来处理 ResponseStatusException 类型的异常,或者使用了 @ResponseStatus 注解标记的普通异常类。我们来看下它的 doResolveException 方法:

@Override

@Nullable

protected ModelAndView doResolveException(

HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {

try {

if (ex instanceof ResponseStatusException) {

return resolveResponseStatusException((ResponseStatusException) ex, request, response, handler);

}

ResponseStatus status = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class);

if (status != null) {

return resolveResponseStatus(status, request, response, handler, ex);

}

if (ex.getCause() instanceof Exception) {

return doResolveException(request, response, handler, (Exception) ex.getCause());

}

}

catch (Exception resolveEx) {

}

return null;

}

可以看到,首先判断异常类型是不是 ResponseStatusException,如果是,则直接调用 resolveResponseStatusException 方法进行异常信息处理,如果不是,则去查找到异常类上的 @ResponseStatus 注解,并从中查找出相关的异常信息,然后调用 resolveResponseStatus 方法进行处理。

可以看到,ResponseStatusExceptionResolver 处理的异常类型有两种:

  • 直接继承自 ResponseStatusException 的异常类,这种异常类可以直接从里边提取出来想要的信息。

  • 通过 @ResponseStatus 注解的普通异常类,这种情况下异常信息从 @ResponseStatus 注解中提取出来。

这个比较简单,没啥好说的。

2.4 SimpleMappingExceptionResolver

SimpleMappingExceptionResolver 则是根据不同的异常显示不同的 error 页面。可能有的小伙伴还没用过 SimpleMappingExceptionResolver,所以松哥这里先简单说一下用法。

SimpleMappingExceptionResolver 的配置非常简单,直接提供一个 SimpleMappingExceptionResolver 的实例即可,如下:

@Bean

SimpleMappingExceptionResolver simpleMappingExceptionResolver() {

SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();

Properties mappings = new Properties();

mappings.put(“java.lang.ArithmeticException”, “11”);

mappings.put(“java.lang.NullPointerException”, “22”);

resolver.setExceptionMappings(mappings);

Properties statusCodes = new Properties();

statusCodes.put(“11”, “500”);

statusCodes.put(“22”, “500”);

resolver.setStatusCodes(statusCodes);

return resolver;

}

在 mappings 中配置异常和 view 之间的对应关系,要写异常类的全路径,后面的 11、22 则表示视图名称;statusCodes 中配置了视图和响应状态码之间的映射关系。配置完成后,如果我们的项目在运行时抛出了 ArithmeticException 异常,则会展示出 11 视图,如果我们的项目在运行时抛出了 NullPointerException 异常,则会展示出 22 视图。

这是用法,了解了用法之后我们再来看源码,就容易理解了,我们直接来看 doResolveException 方法:

@Override

@Nullable

protected ModelAndView doResolveException(

HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {

String viewName = determineViewName(ex, request);

if (viewName != null) {

Integer statusCode = determineStatusCode(request, viewName);

if (statusCode != null) {

applyStatusCodeIfPossible(request, response, statusCode);

}

return getModelAndView(viewName, ex, request);

}

else {

return null;

}

}

  1. 首先调用 determineViewName 方法确定视图的名称。

总结

阿里伤透我心,疯狂复习刷题,终于喜提offer 哈哈~好啦,不闲扯了

image

1、JAVA面试核心知识整理(PDF):包含JVMJAVA集合JAVA多线程并发,JAVA基础,Spring原理微服务,Netty与RPC,网络,日志,ZookeeperKafkaRabbitMQ,Hbase,MongoDB,Cassandra,设计模式负载均衡数据库一致性哈希JAVA算法数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算共30个章节。

image

2、Redis学习笔记及学习思维脑图

image

3、数据面试必备20题+数据库性能优化的21个最佳实践

image

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

AVA集合**,JAVA多线程并发,JAVA基础,Spring原理微服务,Netty与RPC,网络,日志,ZookeeperKafkaRabbitMQ,Hbase,MongoDB,Cassandra,设计模式负载均衡数据库一致性哈希JAVA算法数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算共30个章节。

[外链图片转存中…(img-RiSDyIMG-1715226563235)]

2、Redis学习笔记及学习思维脑图

[外链图片转存中…(img-xP0OxA5k-1715226563235)]

3、数据面试必备20题+数据库性能优化的21个最佳实践

[外链图片转存中…(img-9SO5RoEH-1715226563235)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值