Java新手技术问询:如何实现HTTP GET请求输出与CSV列表的列匹配校验
实现HTTP GET结果与CSV全列匹配校验的思路和代码优化
嘿,作为Java新手能写出这样的Spring Boot测试代码已经很赞啦!针对你要实现全列匹配校验或者统计匹配列数的需求,我来给你梳理下具体思路和代码修改方案:
核心思路拆解
- 第一步:正确解析CSV全列数据:你原来的代码只处理了前两列,现在要把第三列
technology也读进来,同时要注意跳过CSV的表头行(不然表头会被当成数据对比,导致错误)。 - 第二步:两种匹配需求的实现:
- 全列匹配:可以用
JSONAssert直接对比整个JSON数组,或者手动遍历每个对象的所有字段; - 统计匹配列数:需要把接口返回的JSON转换成对象,逐个字段对比并计数。
- 全列匹配:可以用
- 小提示:尽量不要用
split(",")解析CSV,如果列值里包含逗号(比如"Product, X"),会直接出错,推荐用专业的CSV解析库(比如OpenCSV)来处理。
修改后的完整代码示例
1. 完善数据对象类
首先把yourJson类改成包含所有三列的对象,记得加getter方法(不然JSON序列化时会拿不到字段值):
private class YourJson { private String swProduct; private String swProductModule; private String technology; public YourJson(String swProduct, String swProductModule, String technology) { this.swProduct = swProduct.trim(); // 顺便去掉字符串前后空格,避免格式问题 this.swProductModule = swProductModule.trim(); this.technology = technology.trim(); } // 必须添加getter,否则JSON序列化会失败 public String getSwProduct() { return swProduct; } public String getSwProductModule() { return swProductModule; } public String getTechnology() { return technology; } }
2. 正确读取CSV全列数据(跳过表头)
修改CSV读取逻辑,跳过第一行表头,同时处理全三列:
@Test public void testRetrieveDetails() throws Exception { String csvFilePath = "csv--file--path"; // 读取CSV并转换为预期对象列表,跳过表头行 List<YourJson> expectedList = Files.readAllLines(Paths.get(csvFilePath)) .stream() .skip(1) // 跳过第一行的表头 .map(line -> { String[] parts = line.split(","); // 校验每行的列数,避免数组越界 if (parts.length != 3) { throw new IllegalArgumentException("CSV格式错误,行内容:" + line); } return new YourJson(parts[0], parts[1], parts[2]); }) .collect(Collectors.toList()); JSONArray expected = new JSONArray(expectedList); // 发送GET请求获取实际结果 RequestBuilder requestBuilder = MockMvcRequestBuilders .get("/swproduct/list") .accept(MediaType.APPLICATION_JSON); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); String actualJsonStr = result.getResponse().getContentAsString();
3. 实现全列匹配校验
如果要判断所有列是否完全匹配,直接用JSONAssert即可(注意第三个参数false表示忽略字段顺序,如果你需要严格顺序可以改成true):
// 全列匹配校验 JSONAssert.assertEquals(expected, actualJsonStr, false); System.out.println("所有列完全匹配!");
4. 实现匹配列数统计
如果要统计匹配的列数和完全匹配的行数,可以把实际结果转换成对象列表,逐个字段对比:
// 统计匹配列数逻辑 JSONArray actualJsonArray = new JSONArray(actualJsonStr); List<YourJson> actualList = new ArrayList<>(); // 把接口返回的JSON转换成YourJson对象列表 for (int i = 0; i < actualJsonArray.length(); i++) { JSONObject obj = actualJsonArray.getJSONObject(i); // 这里的字段名要和接口返回的JSON字段完全一致! YourJson actual = new YourJson( obj.getString("swProduct"), obj.getString("swProductModule"), obj.getString("technology") ); actualList.add(actual); } int totalMatchedColumns = 0; int fullyMatchedRows = 0; // 遍历对比每个对象的字段 for (int i = 0; i < Math.min(expectedList.size(), actualList.size()); i++) { YourJson expectedObj = expectedList.get(i); YourJson actualObj = actualList.get(i); int columnMatchCount = 0; // 逐个字段对比 if (expectedObj.getSwProduct().equals(actualObj.getSwProduct())) { columnMatchCount++; } if (expectedObj.getSwProductModule().equals(actualObj.getSwProductModule())) { columnMatchCount++; } if (expectedObj.getTechnology().equals(actualObj.getTechnology())) { columnMatchCount++; } totalMatchedColumns += columnMatchCount; if (columnMatchCount == 3) { fullyMatchedRows++; } // 打印每行的匹配情况(可选) System.out.printf("第%d行:匹配%d列%n", i + 1, columnMatchCount); } // 输出统计结果 System.out.printf("总匹配列数:%d%n", totalMatchedColumns); System.out.printf("完全匹配的行数:%d%n", fullyMatchedRows); }
进阶优化:用专业CSV库避免解析错误
如果你的CSV数据可能包含逗号或者复杂格式,推荐使用OpenCSV库来解析,避免手动split的问题。首先在pom.xml添加依赖:
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>5.6</version> <scope>test</scope> </dependency>
然后修改CSV读取逻辑:
CSVReader csvReader = new CSVReader(new FileReader(csvFilePath)); List<String[]> allRows = csvReader.readAll(); allRows.remove(0); // 跳过表头 List<YourJson> expectedList = allRows.stream() .map(parts -> new YourJson(parts[0], parts[1], parts[2])) .collect(Collectors.toList());
内容的提问来源于stack exchange,提问作者Indranil
相关产品推荐
相关产品推荐

