假设我有一个树结构,例如
类别:
Category1
Subcategory11
Subcategory12
Category2
Subcategory21
Subcategory22
...
类别和子类别是相同的数据类型(类别)。可能存在 > 2 层外壳。
表示返回所有顶级类别的查询的最佳方式是什么?像 www.mysite.com/api/categories 或 www.mysite.com/api/categories/top 这样的东西有用吗?
假设我想在一次 REST 调用中返回整个类别列表,该端点是什么以及这样的要求是否会违反 REST 原则?我可以使用 www.mysite.com/api/categories/tree 之类的东西吗?
顺便说一句,我确实看到了 How should a REST URL schema look like for a tree hierarchy? ,但这不是同质节点树的纯粹示例。
最佳答案
我认为你需要 DFS 算法和这个实现示例.... ( https://www.cis.upenn.edu/~matuszek/cit594-2003/Examples/TreeTraversals/TreeTraversals.java )
But You can pass this method all Category and get tree of this Category. And additionally I want to mention that, you can use CategoryDTO and relative CategoryMapper to represent better style on response (on representation layer e.g. - restful)
//为了便于阅读,省略了 setter、getter 和其他字段(和代码部分)
public class Category implements Serializable, Comparable<Category> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "created_on")
private Instant createdOn;
@Column(name = "created_by")
private String createdBy;
@Column(name = "status")
private Integer status;
@Column(name = "name")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
private Category parent;
@Transient
private List<Category> child = new ArrayList<>();
}
this is method which returns list of Category tree
private List<Category> createTreeCategoriesByParent(final List<Category> categories, Long parentId) {
List<Category> siblings;
siblings = new ArrayList<>();
categories.forEach(category -> {
if (Objects.equals(category.getParent() == null ? null : category.getParent().getId(), parentId)) {
List<Category> children = createTreeCategoriesByParent(categories, category.getId());
children.sort(Category::compareTo);
category.setChild(children);
siblings.add(category);
}
});
return siblings;
}
Your input Category will
[
{
"status": 1,
"name": "Woman",
"accOrder": 0,
"parentId": null,
"child": []
},
{
"status": 1,
"name": "Man",
"accOrder": 0,
"parentId": null,
"child": []
},
{
"status": 1,
"name": "Shoes",
"accOrder": 0,
"parentId": 1,
"child": []
},
{
"status": 1,
"name": "Bijoux",
"accOrder": 1,
"parentId": 1,
"child": []
},
{
"status": 1,
"name": "Sneckers",
"accOrder": 1,
"parentId": 3,
"child": []
},
{
"status": 0,
"name": "Kids",
"accOrder": 1,
"parentId": null,
"child": []
},
{
"status": 1,
"name": "Good Sneckers",
"accOrder": 1,
"parentId": 5,
"child": []
},
{
"status": 1,
"name": "Bad Snackers",
"accOrder": 2,
"parentId": 5,
"child": []
}]
Output is
[
{
"status": null,
"name": "Woman",
"accOrder": 0,
"parentId": null,
"child": [
{
"status": null,
"name": "Shoes",
"accOrder": 0,
"parentId": 1,
"child": [
{
"status": null,
"name": "Sneckers",
"accOrder": 1,
"parentId": 3,
"child": [
{
"createdBy": null,
"status": null,
"name": "Good Sneckers",
"accOrder": 1,
"parentId": 5,
"child": []
},
{
"status": null,
"name": "Bad Snackers",
"accOrder": 2,
"parentId": 5,
"child": []
}
]
}
]
},
{
"status": null,
"name": "Bijoux",
"accOrder": 1,
"parentId": 1,
"child": []
}
]
},
{
"status": null,
"name": "Man",
"accOrder": 0,
"parentId": null,
"child": [
{
"status": null,
"name": "T-shirt",
"accOrder": 1,
"parentId": 2,
"child": []
}
]
},
{
"status": null,
"name": "Kids",
"accOrder": 1,
"parentId": null,
"child": []
},
{
"status": null,
"name": "Furniture",
"accOrder": 4,
"parentId": null,
"child": []
}
]
关于java - 将 REST 中的树数据结构表示为 URL 序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41593828/