java - 将 REST 中的树数据结构表示为 URL 序列

标签 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 之类的东西吗?

顺便说一句,我确实看到了 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/

相关文章:

rest - 如何使用 curl 将包含元数据的文件上传到 jfrog artifactory

python - 修改 Python 代码以使用 SSL 进行 REST 调用

rest - MarkLogic REST API 实例能否独立于其底层数据库进行扩展?

rest - 默认 JSON 到对象映射不起作用

java - nextLine() 在 while 循环结束时跳过行。丢失数据

java - scala play 框架中的自定义消息属性,用于删除硬编码字符串

java - java中的两个线程调用join

Java输入一直为空?

java - 使用嵌套 For 循环来访问 "Add"两个包含数字值的字符串数组

java - 在 Spring 中处理嵌套 REST 资源调用的更优雅的方式?