Home > OS >  Recursive call works only for super parent and not for children
Recursive call works only for super parent and not for children

Time:10-22

I am trying to get the list of strings from a REST response like below:

private List<String> recursiveRestCall(String folderId) throws JsonMappingException, JsonProcessingException {

    List<String> fileList = new ArrayList<>();

    Mono<String> mono = webClient.get().uri("/some/api/"   folderId   "/endpoint/goes/here").retrieve()
            .bodyToMono(String.class);

    final ObjectNode node = new ObjectMapper().readValue(mono.block(), ObjectNode.class);

    if (node.get("parent").get("children").isArray()) {

        for (JsonNode jsonNode : node.get("parent").get("children")) {

            if (jsonNode.get("child").get("isFile").asBoolean()) {
                fileList.add(jsonNode.toString());
            }

            if (jsonNode.get("child").get("isFolder").asBoolean()) {
                recursiveRestCall(jsonNode.get("child").get("id").toString().replaceAll("\"", "")); // This is not working and no error whatsoever.
            }
        }
        return fileList;
    }
    return null;
}

Now, as highlighted in the comment in above snippet. Only the if (jsonNode.get("child").get("isFile").asBoolean()) { condition is executed and I get the list items. I know there are few subfolders having files in them. So effectively the super parent's children are retrieved. But not from the child who is be a parent (as subfolder) too.

What I am missing here?

CodePudding user response:

Save result of this like=

List<String> tempList = recursiveRestCall(jsonNode.get("child").get("id").toString().replaceAll("\"", ""));
if(fileList !=null) 
    list.addAll(tempList);

Based on the comments, You can edit this method like below:

private List<String> recursiveRestCall(String folderId) throws JsonMappingException, JsonProcessingException {

List<String> fileList = new ArrayList<>();

Mono<String> mono = webClient.get().uri("/some/api/"   folderId   "/endpoint/goes/here").retrieve()
        .bodyToMono(String.class);

final ObjectNode node = new ObjectMapper().readValue(mono.block(), ObjectNode.class);

if (node.get("parent").get("children").isArray()) {

    for (JsonNode jsonNode : node.get("parent").get("children")) {

        if (jsonNode.get("child").get("isFile").asBoolean()) {
            fileList.add(jsonNode.toString());
        }

        if (jsonNode.get("child").get("isFolder").asBoolean()) {
            fileList.addAll(recursiveRestCall(jsonNode.get("child").get("id").toString().replaceAll("\"", ""))); // This is not working and no error whatsoever.
        }
    }
    return fileList;
}
return fileList;

}

  •  Tags:  
  • java
  • Related