当前位置:网站首页>Unified exception handling causes ResponseBodyAdvice to fail
Unified exception handling causes ResponseBodyAdvice to fail
2022-07-30 09:43:00 【AE86Jag】
Background
Under the microservice architecture, some basic functions are planned to be extracted into the public Jar package, including unified exception handling, JwtToken verification, unified request response processing, etc. After the extraction, it is found that when an exception occurs, the unified exception capture method is used.Logic, but all the custom ResponseBodyAdvice in the project are not executed, decided to debug step by step to locate the reason, the project is Springboot 2.3.7.RELEASE version.
Location Process
First, test the normal interface and find that the ResponseBodyAdvices are all valid and executed in the order of settings, indicating that these ResponseBodyAdvices are all registered to the container.
Then guess whether ResponseBodyAdvice has been executed but the beforeBodyWrite method has not been executed, and it has returned when the supports method is executed, so a breakpoint is placed in the supports method to see if it will go to the supports method when an exception is thrown, and finally found that allWithout taking the supports method, I guess it may be because of some rules that the ResponseBodyAdvice is filtered.
Continue to DEBUG to see how ResponseBodyAdvice takes effect in the normal process, because a breakpoint has been set in the supports method before, and the interface is directly requested to see the call stack before the supports method, as shown in the figure

Mainly the first few classes, the top of the stack is my custom ResponseBodyAdvice, the breakpoint is on the supports method, the following is line 140 of the processBody method of RequestResponseBodyAdviceChain, then line 116 of the beforeBodyWrite method of RequestResponseBodyAdviceChain, and line 268 of writeWithMessageConverters of the AbstractMessageConverterMethodProcessor class, break points on these methods respectively:

The following will take the beforeBodyWrite method of RequestResponseBodyAdviceChain, continue to go in and see

The inside of the for loop is to actually call each advice to process the returned results. To filter, it can only be done in the getMatchingAdvice method, and continue DEBUG

getAdvice() is to get all the Advice in the system. Because the custom ResponseBodyAdvice is decorated with @ControllerAdvice annotation, it is all ControllerAdviceBean type, and it takes 157 lines of logic.The parameter parameter is the method parameter that finally returns the data. Here I am the method of unified exception handling.Continue to enter

You can see that this method calls the test method of beanTypePredicate. There are three properties in beanTypePredicate, one of which is basePackages. In the custom ResponseBodyAdvice @RestControllerAdvice specifies the package name, and the other two assignableTypes and annotations are also the annotationsAttributes.Continue to see the content of the test method

At this point, you can probably know the reason, that is, the package name does not match, and ResponseBodyAdvice can only process the return value under the specified package name.The other two properties can implement the function of the specified annotation or the Class class to implement the ResponseBodyAdvice:
- annotations attribute, if the returned method is not in the basePackages package, specify this annotation directly on the method
- assignableTypes attribute, if it is not in the basePackages package and there is no annotations annotation, you can also specify the Class class
reason
The package name of the unified exception handling class is not under the package name specified by the basePackages attribute of the @RestControllerAdvice annotation, so the custom ResponseBodyAdvice cannot process the data returned by the unified exception handling
Workaround
- Modify the package name, the package name of basePackages contains the class for unified exception handling
- Add a custom annotation, decorate it on the exception handling method, and write this annotation on the annotations attribute of the @RestControllerAdvice annotation
- Write the class object of exception handling on the assignableTypes attribute of the @RestControllerAdvice annotation
Summary
See the source code for frame exceptions, debug more, and gain a lot.
边栏推荐
- Detailed description of iperf3 parameter options
- 虚幻引擎图文笔记:could not be compiled. Try rebuilding from source manually.问题的解决
- els 方块、背景上色
- The difference between DDR, GDDR, QDR
- 自动化测试selenium(一)
- C language classic practice questions (3) - "Hanoi Tower (Hanoi)"
- 02-课程发布
- 内卷下的智能投影行业,未来何去何从?
- Circuit analysis: constant current source circuit composed of op amp and triode
- Using IN in MySQL will not go through index analysis and solutions
猜你喜欢
随机推荐
Apache DolphinScheduler's new generation of distributed workflow task scheduling platform in practice - Part 1
团队级敏捷真的没你想的那么简单
一个低级错误导致的诡异现象——走近科学能拍三集,(C语言)最简单的数组元素读取,不正确!?
2022杭电多校第二场
XP电源维修fleXPower电源X7-2J2J2P-120018系列详解
Unable to locate the program input point ucrtbase.abort on the dynamic link library api-ms-win-crt-runtime-|1-1-0.dll
els 方块、背景上色
MySQL中使用IN 不会走索引分析以及解决办法
新手必备!最全电路基础知识讲解
STM8L_库函数-模板搭建
MySQL [operator]
One article to understand twenty kinds of switching power supply topologies
利用R语言读取csv文件入一个数据框,然后查看各列的属性。
Concise Notes on Integrals - Types of Curve Integrals of the First Kind
Google Cloud Spanner的实践经验
电源完整性基础知识
js柯里化
20220728 Use the bluetooth on the computer and the bluetooth module HC-05 of Huicheng Technology to pair the bluetooth serial port transmission
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上
How to implement Golang DES encryption and decryption?









