Java 接口本地mock文本结构
# 1. 应用场景
一般针对于后端调用远程接口,而本地无法正常联调的时候,可以通过这段代码,直接返回本地的 text 文件内的 mock 数据接口
# 2. 代码详情
@Override
public JSONObject getTaskItem(String taskId,String isAlarm) throws BusinessException {
try {
BufferedReader reader = new BufferedReader(new FileReader("/Users/fang/Downloads/222222222222222222/flow.txt"));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String string = stringBuilder.toString();
JSONObject jsonObject = JSONObject.parseObject(string);
return jsonObject.getJSONObject("data");
} catch (IOException e) {
throw new RuntimeException(e);
}
// JSONObject param = new JSONObject();
// param.put("taskId",taskId);
// param.put("skipCount",1);
// param.put("maxResultCount",2147483647);
// JSONObject returnObj = httpClientUtil.remotePostRequest(openIp + TASK_ITEM_PAGE, param.toJSONString(),isAlarm);
// if (null != returnObj && returnObj.getInteger("code") == 200) {
// return returnObj.getJSONObject("data");
// }else{
// assert returnObj != null;
// throw new BusinessException(400,"调用下游工单平台接口失败:" + returnObj.get("msg"));
// }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
上次更新: 7/1/2024, 2:17:37 AM