如何将GET API获取的两类MissedCall ID传入PUT API请求体?
问题场景
已通过GET API获取到模板ID(代码实现如下),现在需要将获取到的agentMissedCallId和callerMissedCallSmsId传入PUT API的请求体,构造符合格式要求的请求内容。
获取ID的代码实现
List<String> jsonResponse = response.jsonPath().getList("$"); for (int i = 0; i < jsonResponse.size(); i++) { if (getJsonPath(response, "type[" + i + "]").equals("agent_sms_missed_call")) { agentMissedCallId = getJsonPath(response, "id[" + i + "]"); break; } } for (int i = 0; i < jsonResponse.size(); i++) { if (getJsonPath(response, "type[" + i + "]").equals("caller_sms_missed_call")) { callerMissedCallSmsId = getJsonPath(response, "id[" + i + "]"); break; } }
GET API响应示例
[ { "id": 29169, "name": "Template 1", "type": "agent_sms_missed_call" }, { "id": 29170, "name": "Template 2", "type": "caller_sms_missed_call" } ]
期望的PUT请求体格式
{ "sms_template": [ 29169 ], "name": "Hello ", "destination": "hangup||1", "description": "number for department" }
实现方案
1. 基础实现:用HashMap构造请求体
适合简单场景,直接组装键值对后转为JSON:
// 将String类型的ID转为Integer(请求体中ID为数值类型) Integer agentId = Integer.parseInt(agentMissedCallId); // 构造请求体Map Map<String, Object> putRequestBody = new HashMap<>(); putRequestBody.put("sms_template", Arrays.asList(agentId)); putRequestBody.put("name", "Hello "); putRequestBody.put("destination", "hangup||1"); putRequestBody.put("description", "number for department"); // 若需同时传入caller的ID,修改sms_template即可 // putRequestBody.put("sms_template", Arrays.asList(agentId, Integer.parseInt(callerMissedCallSmsId)));
之后将该Map作为请求体,通过HTTP客户端(如RestAssured、OkHttp)发送PUT请求即可。
2. 规范实现:用JSON序列化工具(如Jackson)构造
定义对应请求体的POJO类,通过实例化赋值后转为JSON:
// 定义请求体对应的POJO类 public class PutRequest { private List<Integer> sms_template; private String name; private String destination; private String description; // 生成Getter、Setter方法(或用Lombok的@Data注解简化) public List<Integer> getSms_template() { return sms_template; } public void setSms_template(List<Integer> sms_template) { this.sms_template = sms_template; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } // 实例化并赋值 PutRequest request = new PutRequest(); request.setSms_template(Arrays.asList(Integer.parseInt(agentMissedCallId))); request.setName("Hello "); request.setDestination("hangup||1"); request.setDescription("number for department"); // 转为JSON字符串(使用Jackson的ObjectMapper) ObjectMapper mapper = new ObjectMapper(); String jsonBody = mapper.writeValueAsString(request);
3. 优化ID获取代码
原代码两次循环遍历列表,可简化为单次循环:
List<Map<String, Object>> jsonResponse = response.jsonPath().getList("$"); for (Map<String, Object> item : jsonResponse) { String type = (String) item.get("type"); if ("agent_sms_missed_call".equals(type)) { agentMissedCallId = item.get("id").toString(); } else if ("caller_sms_missed_call".equals(type)) { callerMissedCallSmsId = item.get("id").toString(); } }
内容的提问来源于stack exchange,提问作者Developer
相关产品推荐
相关产品推荐

