Feign遠(yuǎn)程調(diào)用響應(yīng)結(jié)果格式
public class Result<T> {
/**
* 響應(yīng)碼,200為成功
*/
private Integer code;
/**
* 響應(yīng)信息
*/
private String message;
/**
* 響應(yīng)的具體對象
*/
private T data;
}
自定義Feign解碼器
@Component // 注入Spring的IOC容器中,所有的Feign遠(yuǎn)程調(diào)用響應(yīng)生效
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(), "未返回正確的數(shù)據(jù)", response.request());
}
String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
// TODO 進(jìn)行轉(zhuǎn)換(jackson提供json2obj方法實(shí)現(xiàn)Result轉(zhuǎn)換)
Result result = (Result) JsonUtil.json2obj(bodyStr, type);
// TODO 最終響應(yīng)結(jié)果為Result中的泛型對象
return result.data;
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-671896.html
文章來源:http://www.zghlxwxcb.cn/news/detail-671896.html
到了這里,關(guān)于微服務(wù)Feign組件遠(yuǎn)程調(diào)用自定義解碼器的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!