Strike while the iron is hot and then a recursive call, which is also the requirement of the front and back end docking. The front page can expand a row in the table, which needs to meet the multi-hierarchy relationship, so it is more convenient to solve the problem and deal with it in a recursive way. The page requirements are as follows:

After the expansion:

The part circled in blue can be regarded as a child table, i.e. a child node, which can also be expanded, and so on… You can keep recursing indefinitely.

Code:

Entity class

The persistence layer uses JPA, which is more convenient to achieve self-association.

  1. Service class

Dtos (1)

(2) Query records

Query the parent node directly because the child node is already hanging below the parent during self-association.

(3) traversal

The buildApiParamDTO () method converts the entity class to a DTO.

(4) Recursion

ApiParamDTOList is the top-level list, apiParamDTO is the current node traversed, and apiParamDTOs is the child node of the apiParamDTO.

Recursive child nodes, the most important thing about recursion is that you have a condition to exit recursion. So, the first thing we’re going to do here is check if the child exists, if it doesn’t, we’re going to exit, don’t recurse, otherwise we’re going to continue; Because the child node is also a set, the specific logic is as follows:

Step 1: Iterate through the child nodes. I used the java8 stream implementation here.

The second step: The current node childApiParamDTO may also be the parent node. Then the current node is called recursively to obtain the transformed childApiParamDTOs. ChildApiParamDTOs is a child of childApiParamDTO, calling the setChildApiParamDTOs () method.

Step 3: Return the transformed Set to the outer layer and call the Set method.

Effect:

"list":[ { "type":"REQ_PARAM", "param_name":"userName", "param_type":"String", "required":true, "max_length":64, 16 "param_desc" : "user name", "sample_value" : "zhang", "child_params" : [{" type ":" REQ_PARAM ", "param_name" : "age", "Param_type" : "int", "required" : true, "max_length" : 0, "param_desc" : "age 44," "sample_value" : "26", "child_params":[ { "type":"REQ_PARAM", "param_name":"age", "param_type":"int", "required":true, "max_length":0, "Param_desc" : "age of 48," "sample_value" : "26"}, {" type ":" REQ_PARAM ", "param_name" : "age", "param_type" : "int", "Required" : true, "max_length" : 0, "param_desc" : "age 49", "sample_value" : "26"}}, {" type ":" REQ_PARAM." "Param_name" : "age", "param_type" : "int", "required" : true, "max_length" : 0, "param_desc" : "age 17", "sample_value":"26" } ] }, { "type":"REQ_PARAM", "param_name":"userName", "param_type":"String", "required":true, "Max_length:" 64, "param_desc" : "username 45", "sample_value" : "* *"}]Copy the code