当前位置:网站首页>Two ways to pass feign exceptions: fallbackfactory and global processing Get server-side custom exceptions
Two ways to pass feign exceptions: fallbackfactory and global processing Get server-side custom exceptions
2022-08-02 01:01:00 【m0_67392409】
Generally, when we use feign to call the interface, if the service provider has an exception, the caller will generally display
feign.FeignException$BadRequest: status 400 reading
Regardless of the downtime of the server, the corresponding information will be thrown when there is an exception, but the caller cannot obtain it in general.Through testing, we found that if you print the log, you can see itThe log thrown by the service provider, but the caller cannot capture it, will only report a 400 exception.
So if we want to pass exceptions, we can have two ways, but most of them need to be adjusted by the caller.
First, fallbackFactory
FallbackFactory is similar to the standard fuse downgrade, which can bring all the exception information, but we need to convert the exception, and the regular exception still cannot get the body part.So here we use FeignException to force the transfer, and the problem is solved.
Look, we can get the custom error thrown by the server.
At this time, you can convert the json to bean.
Reference:
Server and Client Common
@FeignClient(value = "sq-service", path = "/service",url = "http://localhost:9980", fallbackFactory = TestFactory.class)public interface TestApi {@PostMapping("/test")String test();}@Componentpublic class TestFactory implements FallbackFactory {@Overridepublic TestApi create(final Throwable throwable) {FeignException ex = (FeignException) throwable;JSONObject jsonObject = JSONObject.parseObject(ex.contentUTF8());return new TestApi() {@Overridepublic String test() {return jsonObject.getString("message");}};}}
Attention!If the client wants to fuse and downgrade, it must be configured in the yaml file:
Client:
feign:hystrix:enabled: true
Otherwise cannot enter fallbackFactory
Second, once and for all, global configuration
I solve it directly with a config file!
Compared with fallbackFactory, there is no need for common code, no need to fuse and downgrade, and the server does not need to make any changes, only the client configuration is required.If you can't change the server, try not to move, otherwise, you really want to diss.
Client
@Configurationpublic class FeignErrorDecoder implements ErrorDecoder {@Overridepublic Exception decode(final String methodKey, final Response response) {try {String message = Util.toString(response.body().asReader());try {JSONObject jsonObject = JSONObject.parseObject(message);// Wrap it into your own custom exception, here it is recommended to change it according to your own codereturn new MyException(jsonObject.getString("message"), jsonObject.getInteger("code"));} catch (JSONException e) {e.printStackTrace();}} catch (IOException ignored) {}return decode(methodKey, response);}}
As you can see, this method can directly feedback to the caller,In other words, front-end calls can be directly displayed on the web page.It can be said that it is very direct to tell you that it is abnormal.
Feign's exception delivery is as simple as that. Generally speaking, projects have their own custom exception global classes for reference only.
Global processing is strongly recommended!!!
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
datax与datax-web安装部署
dbeaver连接MySQL数据库及错误Connection refusedconnect处理
H5页面调用微信授权获取code
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
go笔记记录——channel
Pytorch seq2seq 模型架构实现英译法任务
Test Cases: Four-Step Test Design Approach
unity2D横版游戏教程5-UI
PHP to read data from TXT file
随机推荐
管理基础知识18
datax与datax-web安装部署
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
ELK日志分析系统
JS中localStorage和sessionStorage
Business test how to avoid missing?
期货开户手续费的秘密成了透明
AXI4协议介绍
BGP first experiment
2022/08/01 Study Notes (day21) Generics and Enums
管理基础知识17
MLX90640 红外热成像仪测温传感器模块开发笔记(十) 成果展示-红眼睛相机
datagrip连接mysql数据库
PowerBI商学院佐罗BI真经连续剧
Kubernetes — 核心资源对象 — 网络
Mapped Statements collection does not contain value for的解决方法
期货开户交返是行内公开的秘密
MySQL常用语句整理
好的期货公司开户让人省心省钱
FlinkSQL CDC实现同步oracle数据到mysql