简体   繁体   English

Rest api url 设计异常

[英]Rest api url design exceptions

I read some articles and posts regarding to correct (clean) url desing for rest endpoints.我阅读了一些关于更正(清洁)url 设计 rest 端点的文章和帖子。 Anyway there are some doubts and unclear cases for me which I would like to discuss or be advised.无论如何,我想讨论或建议我有一些疑问和不清楚的情况。

Situation情况

Lets say we have got a service, which has got registered users.假设我们有一个服务,它已经注册了用户。 To every users admin can attach custom notes which some text.管理员可以为每个用户附加一些文本的自定义注释。

The problem which I would like to be advised is just simply CRUD endpoints, which might be trivial problem.我想被告知的问题只是简单的CRUD端点,这可能是微不足道的问题。

We will have 4 related endpoints:我们将有 4 个相关的端点:


POST (create)发布(创建)
According to some guidelines we might to design URL like:根据一些指导方针,我们可能会设计 URL 像:
/users/{userId}/notes
And send a payload containing details.并发送包含详细信息的有效负载。 No problems here这里没有问题


GET (read)获取(读取)
Similar case:类似情况:
/users/{userId}/notes
As a response we receive list of notes attached to specific user.作为响应,我们会收到附加到特定用户的注释列表。 No problems here这里没有问题


UPDATE (update)更新(更新)
Similar case:类似情况:
/users/{userId}/notes
As a payload server receive API object with all note details.作为有效负载服务器,接收API object 以及所有note详细信息。
Here I have my first doubts.在这里,我有我的第一个疑问。 Since I receive note object which contains userId: value- should I go strictly with that URL pattern?因为我收到了包含userId: value 的note object - 我应该严格遵守URL模式吗? userId is redundant in that case.在这种情况下, userId是多余的。 Of course I can receive that value in PathVariable and in payload also, but is it a correct way?当然,我也可以在PathVariable和有效负载中接收该值,但这是正确的方法吗? The same situation appear in many other CRUD 's, that's why I would like to know "good practices"同样的情况出现在许多其他CRUD中,这就是为什么我想知道“良好做法”
API object: API object:

{
    "id": 1,
    "userId": 2,
    "text": "Some message"
}

DELETE (delete)删除(删除)
Similar case:类似情况:
/users/{userId}/notes/{noteId}
In fact for delete I would need only noteId information.事实上,对于删除,我只需要noteId信息。 As I think - /users/notes/{noteId} would be enough, but that's mean for DELETE operation I would have different URL .我认为 - /users/notes/{noteId}就足够了,但这对于DELETE操作意味着我会有不同URL It's similar situation like in UPDATE case.这与UPDATE案例中的情况类似。


Question问题

Should I stick strictly to URL naming convention and use /users/{userId}/notes ?我应该严格遵守 URL 命名约定并使用/users/{userId}/notes吗? What is a good practice for simply CRUD operations?简单的CRUD操作有什么好的做法?

You should review Webber 2011 .您应该查看Webber 2011

In REST, we don't have "endpoints", we have "resources".在 REST 中,我们没有“端点”,我们有“资源”。 Your resource model is, in effect, a collection of documents, each with a unique identifier, than can be read (GET) and written (POST/PUT/PATCH).您的资源 model 实际上是一个文档集合,每个文档都有一个唯一标识符,可以读取 (GET) 和写入 (POST/PUT/PATCH)。

Useful work in your domain model is a side effect that happens when somebody edits one of your resources.您域中的有用工作 model 是当有人编辑您的资源时发生的副作用

Machines don't care what spelling conventions we use for our resource identifiers, so we usually choose a spelling that will make things easy for human beings;机器不关心我们对资源标识符使用什么拼写约定,所以我们通常选择一种对人类来说更容易的拼写; usually this means that the identifier is closely related to the name of the document.通常这意味着标识符与文档的名称密切相关。


Since I receive note object which contains userId: value- should I go strictly with that URL pattern?由于我收到包含 userId: value 的注释 object - 我是否应该严格遵守 URL 模式?

Yes.是的。

The broad point (emphasized by Webber in the talk linked above) is that HTTP is an application protocol;广义的观点(韦伯在上面链接的谈话中强调)是 HTTP 是一个应用协议; general purpose components can do useful work provided that we include the right description of what is going on in the request/response metadata.只要我们在请求/响应元数据中包含对正在发生的事情的正确描述,通用组件就可以完成有用的工作。

In particular, the application has caching, and cache-invalidation .特别是,应用程序具有缓存和缓存失效 If you correctly identify which resource is being changed, caches can automatically invalidated previously cached responses.如果您正确识别正在更改的资源,缓存可以自动使以前缓存的响应无效。

This is one reason why we identify specific resources that are being modified by a request, rather than just POSTing all of our edits to a single generic resource.这就是我们识别请求修改的特定资源的原因之一,而不是仅仅将我们所有的编辑发布到单个通用资源。


Should I stick strictly to URL naming convention and use /users/{userId}/notes?我应该严格遵守 URL 命名约定并使用 /users/{userId}/notes 吗?

Yes;是的; this is, in effect, like having a single web page that holds the notes for each user, with admins editing that web page (adding and removing notes).实际上,这就像拥有一个包含每个用户注释的 web 页面,管理员编辑该 web 页面(添加和删除注释)。

  • Since this is an opinionated question so there's no one single answer to it.由于这是一个自以为是的问题,所以没有一个单一的答案。
  • But for the sake of good design this is what i prefer, as long as i can have unique ID for an entity like for notes in your case, it should be seen as an independent entity only.但是为了良好的设计,这是我更喜欢的,只要我可以拥有一个实体的唯一 ID,就像你的案例中的笔记一样,它应该被视为一个独立的实体。
  • suppose you need to play with entity X, you only need ID of X if you choose X/{id} and not IDs of all the things of which X might be child of, so again simply relying of ID of X is much simpler then relying on IDs of everyone from top parent up to X.假设您需要使用实体 X,如果您选择X/{id} ,则只需要 X 的 ID,而不是 X 可能是其子级的所有事物的 ID,因此再次简单地依赖 X 的 ID 会简单得多依赖从最高父级到 X 的每个人的 ID。
  • Imagine if you go towards users/{id}/notes/{id} design and then in future your notes require to add support for comments and likes and what not, In which case now urls will become users/{id}/notes/{id}/comment/{id} and this could go on since almost everything will be connected to a user by some logic.想象一下,如果您 go 面向users/{id}/notes/{id}设计,那么将来您的笔记需要添加对评论和点的支持,在这种情况下,现在 url 将变为users/{id}/notes/{id}/comment/{id}这可能是 go ,因为几乎所有东西都会通过某种逻辑连接到用户。
  • Plus, depending upon language/framework building routes that are these much connected and nested could be complex to work with, specially for other team members.另外,根据语言/框架构建路径,这些连接和嵌套的路径可能很复杂,特别是对于其他团队成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM