In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the methods and differences of SpringBoot handling global unified exceptions". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the methods and differences of SpringBoot handling global unified exceptions".
Preface
When an exception occurs at the backend or when a request error occurs, the front end usually displays as follows
Whitelabel Error PageThis application has no explicit mapping for / error, so you are seeing this as a fallback.
Fri Jun 07 15:38:07 CST 2019There was an unexpected error (type=Not Found, status=404) .No message available
It is very unfriendly for users.
This article mainly explains how to use unified exception handling in SpringBoot applications.
Mode of realization
The first: use @ ControllerAdvice and @ ExceptionHandler to annotate the second: use the ErrorController class to implement.
First: use @ ControllerAdvice and @ ExceptionHandler annotations
@ Slf4j@ControllerAdvicepublic class GlobalExceptionHandler {@ ResponseBody @ ExceptionHandler (NullPointerException.class) public BaseResult globalException (HttpServletResponse response,NullPointerException ex) {log.info ("GlobalExceptionHandler..."); log.info ("error Code:" + response.getStatus ()); BaseResult result = new WebResult (WebResult.RESULT_FAIL, "request error:" + response.getStatus (), "GlobalExceptionHandler:" + ex.getMessage ()); return result;}}
The annotation @ ControllerAdvice indicates that this is a controller enhancement class that will be intercepted when an exception occurs in the controller and meets the interception exception class defined in the class.
You can define the packet path where the intercepted controller is located
@ Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Componentpublic @ interface ControllerAdvice {@ AliasFor ("basePackages") String [] value () default {}; @ AliasFor ("value") String [] basePackages () default {}; Class [] basePackageClasses () default {}; Class [] assignableTypes () default {}; Class
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.