Feign 自定义 ErrorDecoder (捕获 Feign 服务端异常)

2022-08-09 11:36:17

问题描述:

Feign 客户端捕获不到服务端抛出的异常

解决:

微服务之间接口互相调用,Feign客户端需要捕获服务的抛出的异常,并且返回统一错误格式。

@Configuration
public class FeignErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {
        String msg = null;
        try {
            msg = Util.toString(response.body().asReader());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 抛出自定义的业务异常
        throw new BusinessException(CodeEnum.OPERATIONFAIL.getCode(),msg);

    }
}
  • 作者:hanKongbin
  • 原文链接:https://blog.csdn.net/qq_29766917/article/details/115573459
    更新时间:2022-08-09 11:36:17