In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
What are the knowledge points of this article "what are the methods of exception handling in the java springboot framework?" most people do not understand, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what are the methods of exception handling in the java springboot framework?"
Method 1: customize the error page
Everyone who knows about it knows that in the default exception handling mechanism in our springboot, springboot will send a request to the url of / error once there is an exception in the program we are running. And there is also a BasicExceptionController in springboot to handle / error requests, and then jump to the exception page that we display by default! Of course, we can also jump the error exception to our default custom error page, just create an error.html page in the src/main/resources/templates directory, and then add the following code:.
Method 2: @ ExceptionHandle annotation handles exceptions
For the use of this method, we just add the following method to controller, and the code is as follows:
@ ExceptionHandler (value= {java.lang.ArithmeticException.class}) public ModelAndView arithmeticExceptionHandler (Exception e) {ModelAndView mv = new ModelAndView (); mv.addObject ("error", e.toString ()); mv.setViewName ("error"); return mv;}
Method 3: @ ControllerAdvice+@ExceptionHandler annotation handles exceptions
So when using this method, we need to create a global exception class that can handle exceptions, and then after the creation is completed, we can add the @ ControllerAdvice annotation to this class. The code is as follows:
ControllerAdvicepublic class GlobalException {@ ExceptionHandler (value= {java.lang.ArithmeticException.class}) public ModelAndView arithmeticExceptionHandler (Exception e) {ModelAndView mv = new ModelAndView (); mv.addObject ("error", e.toString ()); mv.setViewName ("error"); return mv;}}
Method 4: configure SimpleMappingExceptionResolver to handle exceptions
This method is similar to the above method 3. When using this method, you need to add a method to the global exception to complete the unified handling of the exception class. Here is the related code:
@ Configurationpublic class GlobalException {public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver () {SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver (); Properties properties = new Properties (); properties.put ("java.lang.ArithmeticException", "error"); resolver.setExceptionMappings (properties); return resolver;}}
Method 5: custom HandlerExceptionResolver class handles exceptions
Then this method needs to implement the HandlerExceptionResolver interface in our global exception class. Here is the relevant code:
@ Configurationpublic class GlobalException implements HandlerExceptionResolver {@ Override public ModelAndView resolverException (HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) {ModelAndView mv = new ModelAndView (); if (exception instanceof ArithmeticException) {mv.setViewName ("error");} mv.addObject ("error", exception.toString ()); return mv }} the above is the content of this article on "what are the methods of exception handling in the java springboot framework?" I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.