问题描述
public class MicroShopDecode implements Decoder {
private final ObjectMapper objectMapper;
public MicroShopDecode(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
public Object decode(Response response,Type type) throws IOException,DecodeException,FeignException {
if (response.status() == 404) {
return Util.emptyValueOf(type);
}
if (response.body() == null) {
return null;
}
Reader reader = response.body().asReader();
if (!reader.markSupported()) {
reader = new BufferedReader(reader,1);
}
try {
// Read the first byte to see if we have any data
reader.mark(1);
if (reader.read() == -1) {
return null;
// Eagerly returning null avoids "No content to map due to end-of-input"
}
reader.reset();
JsonNode node = objectMapper.readTree(reader);
int errcode = node.get("errcode").asInt();
int total_num = node.get("total_num").asInt();
String errmsg = null;
if (node.has("orders")){
// doSomething
JsonNode orders = node.get("orders");
String content = orders.toString();
List<MicroShopOrder> list = objectMapper.readValue(content,List.class);
MicroShopResponseRoot<MicroShopOrder> microShopOrderMicroShopResponseRoot = new MicroShopResponseRoot<>(errcode,list,total_num,errmsg);
return microShopOrderMicroShopResponseRoot;
}
if (node.has("spus")) {
List<MicroShopSpu> list = new ObjectMapper().readValue(node.get("orders").textValue(),List.class);
return new MicroShopResponseRoot<>(errcode,errmsg);
}
} catch (RuntimeJsonMappingException e) {
if (e.getCause() != null && e.getCause() instanceof IOException) {
throw IOException.class.cast(e.getCause());
}
throw e;
}
return null;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)