Java removes the null field returned to the front end

Business background: Recently, we are building a tree structure jointly with the front-end. The back-end uses Java, and the front-end uses Vue. When the back-end returns data to the front-end, because some children fields (children is a collection) are Empty (not null). At this time, a tree pattern will appear on the front end, but there is no data in it. The business requirement is that if the children of the last layer have no data, then the entire layer is not needed. The front-end little sister told me that she displayed the tree according to the presence or absence of the children field, so let the back-end implement it

It is relatively simple to implement here, just add the annotation @JsonInclude(JsonInclude.Include.NON_NULL) to the children field . This annotation means that if the field is null, then this field will not be returned to the front end. Therefore, when writing logic, if there is no data in the children, just set the children collection to null, and the requirement is fulfilled at this point.
insert image description here
Test Results
insert image description here

Explanation: After finishing, you can play happily with the front-end little sister. The above is the record of the author's essays. If there is anything inappropriate, I hope the gods will not hesitate to tell me, and the author will accept it with an open mind! !

Guess you like

Origin blog.csdn.net/qq_41774102/article/details/127772143