Feign 自定义 ErrorDecoder (捕获 Feign 服务端异常)
问题描述:Feign 客户端捕获不到服务端抛出的异常解决:微服务之间接口互相调用,Feign客户端需要捕获服务的抛出的异常,并且返回统一错误格式。@Configurationpublic class FeignErrorDecoder implements ErrorDecoder {@Overridepublic Exception decode(String methodKey, Respon
·
问题描述:
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);
}
}
更多推荐
已为社区贡献3条内容
所有评论(0)