RESTful API路由设计:嵌套与非嵌套[英] RESTful API routes design: nested vs. non-nested

本文是小编为大家收集整理的关于RESTful API路由设计:嵌套与非嵌套的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我的问题是在为API目的构建URL时嵌套资源的优势.考虑以下两个替代方法来访问员工资源:

/api/employees?department=1   # flat

Vs.

/api/departments/1/employees  # nested

现在考虑开发通用库以从API访问REST资源的任务.如果所有路由都是平坦的,则需要知道所访问的资源的名称:

store.query('employees', {department_id:1})   =>   /api/employees?department=1
但是,如果我们要支持嵌套路由,则此包装器需要了解有关嵌套和在哪些资源下的额外信息,以便知道如何构建引用此类模型的URL.鉴于并非所有模型都在相同的父资源下嵌套,甚至一些模型根本不会嵌套,其余包装器库需要具有某种配置,描述否则不需要的所有这些额外知识.

所以我的问题是:

  • 在API中嵌套资源路由有任何实际优势吗? (这并不意味着最终用户消耗,因此越来越少收到更漂亮的URL).

  • 是嵌套的方法真的比平坦更好,超越美学,以便为推出的额外努力和复杂性支持资源URL建设中缺乏统一性?

另请参见: https://stackoverflow.com/a/36410780/621809

更新:重要澄清

我从一些评论和答案中实现了一些评论和答案,我对一个方面并不清楚:我不是通过像/employees/5或/departments/1等网址来解决单个资源.我不认为是嵌套.

当我说嵌套资源时,我指的是/departments/1/employees,其中始终在另一个资源的上下文中寻址资源.主要问题是用于URL建筑,一般库需要了解额外的东西,如"员工在部门嵌套"但"分支不嵌套在任何内容下".如果可以饱于解决所有资源,但以平坦的方式解决,知道如何解决它们是更简单和更可预测的.

当您考虑它时,在数据库中,您不需要知道额外信息,以便知道如何解决对象的集合(例如,在RDMS中的表).您始终将员工的集合称为employees,而不是departments/5/employees.

推荐答案

如果您想做更多的级别,会发生什么?

/api/addresses?departmentId=1&employeeId=2&addressId=3

vs

/api/departments/1/employees/2/addresses/3

地址端点突然与参数膨胀.

此外,如果您正在查看 Richardson成熟度模型级别3 ,Rentful API是可通过链接发现的.例如,从顶级级别,Say/API/版本(/1),您可以发现该部门的链接.这是如何在HAL浏览器这样的工具中查看:

"api:department-query": {
  "href": "http://apiname:port/api/departments?pageNumber={pageNumber}&pageSize={pageSize}&sort={sort}"
},
"api:department-by-id": {
  "href": "http://apiname:port/api/departments?departmentId={departmentId}"
}

(可能是所有列出的查询,最终以分页方式或将直接转到特定部门的参数化链接,或者提供了您知道ID).

这里的优势是客户端只需要知道关系(链接)名称,而服务器将主要是自由的,以改变关系(和资源)URL.

其他推荐答案

旧帖子,但对我来说并不令人满意.

取决于您的API.如果您的数据是分层的,则不需要访问资源而不按父母过滤它们,那么嵌套就可以了(而不是嵌套).

如果您的ID很长(GUID),那么你的层次结构很深,或者你需要访问任何资源而不被父母过滤,然后不嵌套是一个很好的选择.

尝试拥有统一的界面,而不具有许多方法来访问相同的资源.

尝试这个链接解释了这一点更好: https://www.moesif.com/blog/technical/api-design/rest-api-design-best-practics-for-sub-and-nested-资源/

其他推荐答案

从我的经验中: Q1.更容易使用,因为关系模型而难以实现 Q2.嵌套在权限和其他潜在检查之前,您可以在步骤下进行级别

本文地址:https://www.itbaoku.cn/post/1937836.html

问题描述

My question is about the advantages of nesting resources when building URLs for API purposes. Consider the following two alternatives for accessing an employee resource:

/api/employees?department=1   # flat

Vs.

/api/departments/1/employees  # nested

Now consider the task of developing a general purpose library to access REST resources from an API. If all routes were flat, such a REST wrapper library would only need to know the name of the resource being accessed:

store.query('employees', {department_id:1})   =>   /api/employees?department=1

However, if we were to support nested routes, this wrapper would need to know extra information about what models are nested and under which other resource, in order to know how to build the URLs for referencing such a model. Given that not all models would be nested under the same parent resource, and even some models would not be nested at all, the REST wrapper library would need to have some sort of configuration describing all this extra knowledge that wouldn't be needed otherwise.

So my questions are:

  • Are there any real advantages to nested resource routes in an API? (Which is not meant to be consumed by end users, and therefore gains less from having prettier URLs).

  • Is the nested approach really better than flat, beyond aesthetics, so as to justify the extra effort and complexity introduced to support the lack of uniformity in resource URL building?

See also: https://stackoverflow.com/a/36410780/621809

UPDATE: IMPORTANT CLARIFICATION

I realize from some of the comments and answers, that I wasn't clear enough regarding one aspect: I'm not against addressing single resources with URLs like /employees/5 or /departments/1. I don't consider that to be nested.

When I say nested resources, I refer to URLs like /departments/1/employees where a resource is addressed always within the context of another resource. The main issue is the fact that for URL building, a generic library would need to know extra stuff like "employees are nested under departments" but "branches are not nested under anything". If all resources could be addressed RESTfully, but in a flat fashion, it is simpler and more predictable to know how to address them.

When you think about it, in databases you don't need to know extra information in order to know how to address a collection of objects (e.g. a table in a RDMS). You always refer to the collection of employees as employees, not as departments/5/employees.

推荐答案

What happens if you want do drill down a couple more levels?

/api/addresses?departmentId=1&employeeId=2&addressId=3

vs

/api/departments/1/employees/2/addresses/3

The Address endpoint suddenly becames bloated with parameters.

Also, if you're looking at the Richardson Maturity Model level 3, RESTful APIs are meant to be discoverable through links. For example, from the top level, say /api/version(/1), you would discover there's a link to the departments. Here's how this could look in a tool like HAL Browser:

"api:department-query": {
  "href": "http://apiname:port/api/departments?pageNumber={pageNumber}&pageSize={pageSize}&sort={sort}"
},
"api:department-by-id": {
  "href": "http://apiname:port/api/departments?departmentId={departmentId}"
}

(either a query that might list them all, in a paged manner eventually, or a parameterized link that would go directly to a specific department, provided you know the id).

The advantage here would be that the client would only need to know the relationship (link) name, while the server would be mostly free to alter the relationship (and resource) url.

其他推荐答案

Old post, but not satisfactory answer for me.

It depends on your API. If your data is hierarchical and do not need to access resources without filtering them by their parents, then nested is OK (and not nested too).

If your ids are long (GUIDs), your hierarchy is deep, or you need to access any resource without filtering by its parents then not nested is a good option.

Try to have a unified interface and not having many ways for accessing the same resource.

Try this link that explains this a lot better: https://www.moesif.com/blog/technical/api-design/REST-API-Design-Best-Practices-for-Sub-and-Nested-Resources/

其他推荐答案

From my experience: Q1. Easier to use, hard to implement because of relational model Q2. Nested is better when it comes to permissions and others potential checks that you can do before you go levels down