Home:ALL Converter>StackOverflowError when generating json tree

StackOverflowError when generating json tree

Ask Time:2016-12-27T10:32:18         Author:qwd412

Json Formatter

Creating online store. I need to generate json that represents category tree, but when Im generating it got StackOverflowError. Category Entity :

@OneToMany
@JoinColumn(name = "parent_category_id")
private List<Category> subcategories;


@ManyToOne
@JoinColumn(name = "parent_category_id")
private Category parentCategory;

Json generating method, I send List from Controller to represent tree

    @Override
public List<Category> getCategoryTree() {
    List<Category> categories = categoryDao.findAll();
    List<Category> roots = categories.stream()
            .filter(category -> category.getParentCategory()!=null)
            .collect(Collectors.toList());
    return roots;
}

I guess its because child gets parent and parent get child. But i cant place @JsonIgnore annotation because then it will write only list of parents without children or all children without parents, is there must be a way to generate json

Author:qwd412,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/41338314/stackoverflowerror-when-generating-json-tree
yy