7.10 SpringBoot Actual Global Exception Handling

CSDN achieves 100 million technicians


foreword

In a project, exception handling is a topic you can't get around!

No matter how awesome you are, the code you write may have exceptions. Some exceptions are very easy to test, but some exceptions will only appear in very extreme situations. Therefore, exceptions are really difficult to deal with!

Once upon a time, you were so confident in your code that you didn't think about doing exception handling, and then you heard that your service was unstable! Reporting from time to time 500服务器内部错误, you can't figure it out at this time, what's the situation?

Then, you think quietly: Soldiers come to block you, water comes to cover you, if you see a move, you can break it, it's not difficult for me! So, you made a very "safe" method: add a try-catch to the code in the controller, and log:

try {
    
    
   // 业务代码
    return TgResult.ok();
} catch(Exception ex) {
    
    
    // 不管任何异常,统统抓到打log,并返回统一结果
   log.error("异常了",ex);
   return TgResult.fail(。。。)
}

Hey, in this way, no exceptions will jump out of my Wuzhishan, and 500 will not be reported, and then the code can be optimized according to the exception log!

But slowly you find that this work is a bit cumbersome, the method try-catch in the controller is very similar, and when the extension handles new exceptions, you have to change dozens or hundreds of places, which is really frustrating!

After modifying and modifying, suddenly a breeze comes, and you vaguely think that with so many similar codes and such similar processing, can you use the filters, interceptors, and AOP you have learned before?

So, you launched a search for Dafa and found the AOP implementation of global exception handling provided by SpringBoot: @RestControllerAdvice!


@RestControllerAdvice project actual combat

You removed all the try-catch in the controller Riga, and created a new GlobalExceptionHandler class

insert image description here

Usage is simple:

  • Adding annotations to the class @RestControllerAdviceis equivalent to @ControllerAdvice + @ResponseBody. By default, all controllers are cut. Of course, you can also @ControllerAdviceselect qualified controllers through some attributes in the annotations.
  • Add methods to handle exceptions, and add @ExceptionHandlerannotations: specify exceptions to handle

Example:

@RestControllerAdvice // 指定全局异常处理类
@Slf4j
public class GlobalExceptionHandler {
    
    

    @ExceptionHandler(Exception.class) // 指定要处理的异常为Exception
    public TgResult ExceptionHandler(Exception e){
    
    
        log.error("ExceptionHandler", e);
        return TgResult.fail("500", "操作失败:" + e.getMessage());
    }
}

In fact, the try-catch added to all controllers is uniformly written in one place in the way of AOP. In this way, if you want to handle any exception, just add the @ExceptionHandler annotation modification method here, and all controllers will be dealt with in terms of exceptions. Isn't it very elegant?

Of course, what I deal with here is relatively general, and actual projects are usually divided to 几个大类deal with corresponding exceptions, and different solutions are given! In addition, it is still necessary to use Exception for unhandled exceptions, so even if it is Exception, we need to refine it,So do you know how to achieve it?

In order to impress you on this part, I will not give the answer here, you first think and practice yourself! Don't think about finding answers from online articles. Many articles on the Internet are copied and copied. They only talk about the global exception handling mechanism, and they don't talk about how to actually fight in depth! So, think hard and I will give the answer below!


at last

If you want to read more practical articles, I still recommend my practical column –> "Based on SpringBoot+SpringCloud+Vue Separation of Front-end and Back-end Projects", a column jointly created by me and the front-end dog brother, which allows you to learn from From 0 to 1, you can quickly have practical experience in enterprise-level standardized projects!

Specific advantages, planning, and technology selection can all be read in " The Beginning "!

After subscribing to the column, you can add my WeChat, and I will provide targeted guidance for each user!

In addition, don't forget to follow me: Tiangang gg , it is not easy to miss new articles: https://blog.csdn.net/scm_2008

Guess you like

Origin blog.csdn.net/scm_2008/article/details/132392466