0
点赞
收藏
分享

微信扫一扫

【微服务】微服务的概论

云卷云舒xj 2023-08-23 阅读 24

Feign远程调用响应结果格式

public class Result<T> {
    /**
     * 响应码,200为成功
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应的具体对象
     */
    private T data;
}

自定义Feign解码器

@Component // 注入Spring的IOC容器中,所有的Feign远程调用响应生效
public class FeignResultDecoder implements Decoder {

    @Override
    public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
        if (response.body() == null) {
            throw new DecodeException(response.status(), "未返回正确的数据", response.request());
        }
        String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
        // TODO 进行转换(jackson提供json2obj方法实现Result转换)
        Result result = (Result) JsonUtil.json2obj(bodyStr, type);
        // TODO 最终响应结果为Result中的泛型对象
        return result.data;
    }

}
举报

相关推荐

0 条评论