时间:2021-05-19
如果以上3个异常解析器都无法处理,会上抛给tomcat,处理异常内部的默认工作流程:所有异常解析器依次尝试解析,解析完成进行后续操作,解析失败,下一个解析器继续尝试解析。
@ExceptionHandler标注在方法上
@ExceptionHandler统一异常处理
@ResponseStatus注解标注在自定义异常上,用于设置自定义异常信息
@ResponseStatus(reason = "用户被拒绝登录", value = HttpStatus.NOT_ACCEPTABLE)public class UsernameNotFoundException extends RuntimeException { private static final long serialVersionUID = 1L;}DefaultHandlerExceptionResolver包含以下默认异常
源码: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; } ...}如下HttpRequestMethodNotSupportedException请求方式不对。使用GET对POST方法进行访问
@RequestMapping(value = "/handle03", method = RequestMethod.POST)public String postMethod() { return "success";}若没有对应异常处理类,会进入对应服务器错误页面
以上就是详解SpringMVC中的异常处理的详细内容,更多关于SpringMVC 异常处理的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文首先将会回顾Spring5之前的SpringMVC异常处理机制,然后主要讲解SpringBoot2Webflux的全局异常处理机制。SpringMVC的异常
前言在SpringMVC中,当一个请求发生异常(Controller抛出一个异常时),DispatcherServlet采用委托的方式交给一个处理链来处理或者解
Java异常处理运行时异常(RuntimeException)详解及实例RuntimeExceptionRunntimeException的子类:ClassCa
这篇文章主要介绍了SpringMVC-统一异常处理三种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
详解React16中的异常处理异常处理在React15.x及之前的版本中,组件内的异常有可能会影响到React的内部状态,进而导致下一轮渲染时出现未知错误。这些