In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "principle Analysis of SpringBoot exception handling". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "principle Analysis of SpringBoot exception handling" can help you solve the problem.
Exception handling flow
Execute the target method. Any exception during the running of the target method will be caught by catch and mark the end of the current request. DispatchException throws an exception.
Enter the view parsing process and render the page. If an exception occurs, the parameter mv is empty, and the captured exception dispatchException is passed in.
Handle the exception that occurs in handler, and return ModelAndView after processing
(1) iterate through all the HandlerExceptionResolvers to find a parser that can handle the current exception to parse the exception
(2) call resolveException to parse the exception, pass in the request and response objects, which method, the exception occurred, and then custom exception handling returns ModelAndView
(3) default exception parser of the system
① DefaultErrorAttributes handles the exception first, saves the exception information to the request field and returns null
② ExceptionHandlerExceptionResolver is used to handle method exceptions marked with @ ExceptionHandler annotations
③ ResponseStatusExceptionResolver is used to handle method exceptions marked with @ ResponseStatus annotations
④ DefaultHandlerExceptionResolver default handler exception parser that handles some common exceptions
(4) if no parser can handle the exception, the exception will be thrown
(5) if no parser can handle the current exception, a / error request will eventually be sent to forward the saved exception information to / error. BasicErrorController specifically handles / error requests, and BasicErrorController traverses all ErrorViewResolver parsing error views. If there is no custom error view parser, the default DefaultErrorViewResolver is used, and the response code is used as the address of the error page, which the template engine finally responds to.
Several methods and principles of exception handling
1. Custom error page, error/404.html, error/5xx.html. If there is an accurate error status code, the page matches exactly. If not, find 4xx.html. If there is none, the white page will be triggered.
two。 Use @ ControllerAdvice and @ ExceptionHandler to handle global exceptions, the underlying is supported by ExceptionHandlerExceptionResolver
3. Use @ ResponseStatus and custom exceptions. The bottom layer is ResponseStatusExceptionResolver, the bottom layer calls response.sendError (statusCode, resolvedReason), and Tomcat receives an error. At the end of the request, new an empty ModelAndView return, so that no parser can handle the current exception, and eventually / error request will be sent. BasicErrorController will specifically handle / error request, adapting to 4xx.html or 5xx.html pages.
4.Spring underlying exceptions, such as parameter type conversion exceptions. The bottom layer is the exception at the bottom of the DefaultHandlerExceptionResolver processing framework, and the bottom layer is also response.sendError (HttpServletResponse.SC_NOT_ACCEPTABLE), and Tomcat receives an error. At the end of the request, new an empty ModelAndView return, so that no parser can handle the current exception, and eventually / error request will be sent. BasicErrorController will specifically handle / error request, adapting to 4xx.html or 5xx.html pages.
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) {if (logger.isWarnEnabled ()) {logger.warn ("Failure while trying to resolve exception [" + ex.getClass (). GetName () + "]", handlerEx) }} return null;}
5. Custom implementation HandlerExceptionResolver handles exceptions, which can be used as the default global exception handling rule
Order (value = Ordered.HIGHEST_PRECEDENCE) @ Componentpublic class CustomerHandlerExceptionResolver implements HandlerExceptionResolver {@ Override public ModelAndView resolveException (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {try {response.sendError (521, "I love you!");} catch (IOException e) {e.printStackTrace ();} return new ModelAndView ();}}
ErrorViewResolver implements custom handling of exceptions.
(1) when response.sendError is called at the bottom, the error request will be transferred to basicErrorController,BasicErrorController by default to handle the / error request, adapting to 4xx.html or 5xx.html pages.
(2) if no parser can handle the exception, the bottom layer of tomcat will also call response.sendError. By default, the error request is forwarded to basicErrorController,BasicErrorController to handle the / error request, adapting to 4xx.html or 5xx.html pages.
(3) the address of the page to which basicErrorController is going is determined by ErrorViewResolver, the wrong view parser, that is, adapting to 4xx.html or 5xx.html pages.
This is the end of the introduction to "the principle Analysis of SpringBoot exception handling". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.