REM:
1. 入参如果为数组,其中包含null,则id数组用null表示
This commit is contained in:
chenlinghy 2020-05-28 17:12:57 +08:00
parent 560944c98b
commit 54398090f5
2 changed files with 12 additions and 2 deletions

View File

@ -30,8 +30,10 @@ public class IDArrayReq {
public void setId(String[] id) { public void setId(String[] id) {
List<String> idList = Arrays.asList(id); List<String> idList = Arrays.asList(id);
if(idList.contains("")) { if (idList.contains("")) {
this.id = new String[]{""}; this.id = new String[]{""};
} else if (idList.contains(null)) {
this.id = null;
} else { } else {
Map<String, Long> ret = idList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); Map<String, Long> ret = idList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(ret); System.out.println(ret);
@ -44,8 +46,10 @@ public class IDArrayReq {
public void setTaskId(String[] taskId) { public void setTaskId(String[] taskId) {
List<String> idList = Arrays.asList(taskId); List<String> idList = Arrays.asList(taskId);
if(idList.contains("")) { if (idList.contains("")) {
this.taskId = new String[]{""}; this.taskId = new String[]{""};
} else if (idList.contains(null)) {
this.taskId = null;
} else { } else {
Map<String, Long> ret = idList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); Map<String, Long> ret = idList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(ret); System.out.println(ret);

View File

@ -16,13 +16,19 @@ public class demo {
String json = "{\"id\":[\"1\", \"123\", \"1234\", \"1234\"]}"; String json = "{\"id\":[\"1\", \"123\", \"1234\", \"1234\"]}";
String json2 = "{\"id\":[\"1\", \"123\", \"1234\"]}"; String json2 = "{\"id\":[\"1\", \"123\", \"1234\"]}";
String json3 = "{\"id\":[\"\", \"1\", \"123\", \"1234\"]}"; String json3 = "{\"id\":[\"\", \"1\", \"123\", \"1234\"]}";
String json4 = "{\"id\":[\"1\", \"123\", \"1234\", null]}";
IDArrayReq id = new ObjectMapper().readValue(json3, IDArrayReq.class); IDArrayReq id = new ObjectMapper().readValue(json3, IDArrayReq.class);
Assert.assertEquals(id.getId().length, 1); Assert.assertEquals(id.getId().length, 1);
id = new ObjectMapper().readValue(json2, IDArrayReq.class); id = new ObjectMapper().readValue(json2, IDArrayReq.class);
Assert.assertEquals(id.getId().length, 3); Assert.assertEquals(id.getId().length, 3);
id = new ObjectMapper().readValue(json, IDArrayReq.class); id = new ObjectMapper().readValue(json, IDArrayReq.class);
Assert.assertEquals(id.getId().length, 3); Assert.assertEquals(id.getId().length, 3);
id = new ObjectMapper().readValue(json4, IDArrayReq.class);
Assert.assertEquals(id.getId().length, 1);
} }
} }