在REST中将树数据结构表示为URL序列

时间:2017-01-11 14:45:13

标签: java rest

假设我有一个树结构,比如

分类

  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吗?

BTW,我确实看到了How should a REST URL schema look like for a tree hierarchy?,但这不是同构节点树的纯粹例子。

1 个答案:

答案 0 :(得分:0)

我认为您需要DFS算法和此实现示例....(https://www.cis.upenn.edu/~matuszek/cit594-2003/Examples/TreeTraversals/TreeTraversals.java

  

但是您可以将此方法传递给所有类别并获取此树   类别。另外我想提一下,你可以使用CategoryDTO和相对的CategoryMapper来表示更好的响应风格(在表示层上,例如 - restful)

//可读性设置器,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<>();

    }
  

这是返回类别树列表的方法

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;
    }
  

您的输入类别

[
  {
    "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": []
  }]
  

输出

[
  {
    "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": []
  }
]