问题描述:

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);

    }
}

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐