外部API响应字段规则变更:代码校验方案咨询及示例需求
问题描述
我正在调用一个外部API,原响应示例如下:
{ "totalInvestedAmount": 10500.0, "totalCurrentValue": 10550.0, "otherInfo": { "firstData" : [ "hi", "hello", "bye" ], "secondData": "secondData", "thirdData": { "a": "aaa", "b": "bbb" } } }
其中otherInfo是包含firstData(String数组)和secondData(字符串)的对象。
此前API的响应规则:若firstData或secondData为空,会返回空数组或空字符串,示例:
{ "totalInvestedAmount": 10500.0, "totalCurrentValue": 10550.0, "otherInfo": { "firstData" : [ "", ""], "secondData": "" } }
我当前代码仅对otherInfo对象和secondData做Objects::nonNull校验。
API响应规则变更
现在该API的响应规则发生变更:
- 若
firstData为空值,响应中将不再包含firstData字段; - 若
secondData为空,响应中将不再包含secondData字段; - 若
firstData和secondData均为空,响应中将不再包含otherInfo对象。
变更后的响应示例:
示例1:firstData为空时的响应
{ "totalInvestedAmount": 10500.0, "totalCurrentValue": 10550.0, "otherInfo": { "secondData": "sample text" } }
示例2:secondData为空时的响应
{ "totalInvestedAmount": 10500.0, "totalCurrentValue": 10550.0, "otherInfo": { "firstData": [ "word1", "word2"] } }
示例3:firstData和secondData均为空时的响应
{ "totalInvestedAmount": 10500.0, "totalCurrentValue": 10550.0 }
现需要明确:针对上述三种变更后的响应场景,代码中需要新增哪些校验逻辑?并提供示例解决方案。
新增校验逻辑与示例代码
针对API的规则变更,需要补充以下校验逻辑:
- 校验
otherInfo是否存在:当firstData和secondData都为空时,响应中不会有otherInfo字段,因此在处理otherInfo下的子字段前,必须先判断otherInfo是否非null。 - 校验
firstData是否存在:因为firstData为空时会被省略,不再返回空数组,所以需要判断firstData是否为null(即是否存在),而非仅判断数组是否为空。 - 结合
otherInfo存在性校验secondData:原有Objects::nonNull校验保留,但需在otherInfo存在的前提下进行判断;若otherInfo不存在,直接认定secondData为空。
Java示例代码(基于Jackson反序列化)
假设你用Jackson将JSON反序列化为Java对象,实体类定义如下:
import lombok.Data; import java.util.List; @Data public class ApiResponse { private Double totalInvestedAmount; private Double totalCurrentValue; private OtherInfo otherInfo; @Data public static class OtherInfo { private List<String> firstData; private String secondData; private ThirdData thirdData; @Data public static class ThirdData { private String a; private String b; } } }
处理响应的校验逻辑示例:
import java.util.Objects; import java.util.Collections; public class ApiResponseHandler { public void handleResponse(ApiResponse response) { // 处理总金额字段(业务逻辑略) Double totalInvested = response.getTotalInvestedAmount(); Double totalCurrent = response.getTotalCurrentValue(); // 新增:校验otherInfo是否存在 ApiResponse.OtherInfo otherInfo = response.getOtherInfo(); if (Objects.nonNull(otherInfo)) { // 新增:处理firstData,不存在则赋值空列表避免空指针 List<String> firstData = otherInfo.getFirstData(); if (Objects.isNull(firstData)) { firstData = Collections.emptyList(); } processFirstData(firstData); // 原有secondData校验,结合otherInfo存在性判断 String secondData = otherInfo.getSecondData(); if (Objects.nonNull(secondData)) { processSecondData(secondData); } else { // secondData不存在时的默认处理,比如赋值空字符串 processSecondData(""); } // 处理thirdData(业务逻辑略) } else { // otherInfo不存在,说明firstData和secondData都为空,执行对应逻辑 processEmptyOtherInfo(); } } private void processFirstData(List<String> data) { // firstData业务逻辑实现 } private void processSecondData(String data) { // secondData业务逻辑实现 } private void processEmptyOtherInfo() { // 双字段为空时的处理逻辑 } }
关键说明
- 对于
firstData:API不再返回空数组,反序列化后会是null,需转换为空列表,避免后续业务逻辑触发空指针异常。 - 对于
secondData:保留原有非空校验,但需在otherInfo存在的前提下执行;若secondData为null,可根据业务需求赋值默认值或执行特定逻辑。 - 对于
otherInfo:新增顶层判断,若为null直接处理双字段为空的场景。
内容的提问来源于stack exchange,提问作者Palak Kharbanda
相关产品推荐
相关产品推荐

