Browse Source

fix(http): 处理非JSON格式的API响应

增加异常捕获逻辑,当API返回非JSON内容时不会解析失败,而是返回默认的成功响应
zhc
zhczyx@163.com 1 month ago
parent
commit
c1b9d95c41
  1. 10
      src/main/java/com/aeon/tcp/utils/DefaultHttpRequestUtil.java

10
src/main/java/com/aeon/tcp/utils/DefaultHttpRequestUtil.java

@ -27,7 +27,15 @@ public class DefaultHttpRequestUtil {
.execute()
.body();
logger.debug("resonponseJson: {}",responseJson);
MyHTTPResponse response = JSONUtil.parseObj(responseJson).toBean(MyHTTPResponse.class);
MyHTTPResponse response = new MyHTTPResponse();
try {
response = JSONUtil.parseObj(responseJson).toBean(MyHTTPResponse.class);
} catch (Exception e) {
logger.warn("API response is not JSON, raw response: {}", responseJson);
response.setCode(200);
response.setMsg("success");
}
return response;
}

Loading…
Cancel
Save