当前位置:网站首页>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 readingRegardless 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: trueOtherwise 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
边栏推荐
- 严格模式,use strict
- Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
- FlinkSQL CDC实现同步oracle数据到mysql
- Redis - message publish and subscribe
- Don't concatenate strings with jOOQ
- 技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
- C语言函数详解(1)【库函数与自定义函数】
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- Realize deletion - a specified letter in a string, such as: the string "abcd", delete the "a" letter in it, the remaining "bcd", you can also pass multiple characters to be deleted, and pass "ab" can
- Business test how to avoid missing?
猜你喜欢

管理基础知识21

PHP to read data from TXT file

ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法

Test Cases: Four-Step Test Design Approach

These 4 computer notepad software, you have to try

datagrip连接mysql数据库

【目标检测】FCOS: Fully Convolutional One-Stage Object Detection

C语言实现扫雷游戏

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

鲲鹏编译调试插件实战
随机推荐
Redis和MySQL数据一致性问题,有没有好的解决方案?
Markdown (CSDN) MD编辑器(四)- 漂亮表格(表格背景色、跨行、跨列)
Kubernetes — 核心资源对象 — 网络
GO开发环境配置
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
管理基础知识18
理解分布式系统中的缓存架构(下)
冒泡排序函数封装
C语言函数详解(1)【库函数与自定义函数】
管理基础知识21
关于MySQL的数据插入(高级用法)
ECMAScript 2022 正式发布,有你了解过的吗?
go笔记——锁
Flink_CDC搭建及简单使用
哪里有期货开户的正规途径?
flask获取post请求参数
期货开户如何确定期货公司正规性?
Redis 相关问题
管理基础知识14
Business test how to avoid missing?