在 URL 中使用动词从根本上与 REST 不兼容吗? [英] Is using a verb in URL fundamentally incompatible with REST?

查看:38
本文介绍了在 URL 中使用动词从根本上与 REST 不兼容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以假设我们有一些东西似乎不能最好地表示为资源(我们想要暂停的进程状态、我们想要在服务器上执行的无状态计算等).

So let's say we have something that does not seem best represented as a resource (status of process that we want to pause, stateless calculation we want to perform on the server, etc).

如果在 API 设计中我们使用 process/123/pausecalculations/fibonacci -- 这是否从根本上与 REST 不兼容?到目前为止,我所读到的似乎没有,只要这些 URL 可以使用 HATEOAS 发现并且媒体类型是标准化的.

If in API design we use either process/123/pause or calculations/fibonacci -- is that fundamentally incompatible with REST? So far from what I read it does not seem to, as long as these URLs are discoverable using HATEOAS and media types are standardized.

或者我应该更喜欢在消息中按照此处的回答进行操作?

Or should I prefer to put action in the message as answered here?

注意 1:
我确实理解可以用名词重新表述我的一些例子.但是我觉得对于特定情况,名词的作用不如动词.所以我试图了解使用这些动词是否会立即变得不稳定.如果是,那么为什么建议如此严格,以及如果在这些情况下不遵循它,我可能会错过哪些好处.

Note 1:
I do understand that it is possible to rephrase some of my examples in terms of nouns. However I feel that for specific cases nouns do not work as well as verbs do. So I am trying to understand if having those verbs would be immediately unRESTful. And if it is, then why the recommendation is so strict and what benefits I may miss by not following it in those cases.

注意 2:
回答 REST 对此没有任何限制" 将是一个有效的答案(这意味着这种方法是 RESTful 的).回答 这取决于你问谁"这是最佳做法" 并没有真正回答问题.这个问题假设 REST 的概念作为一个定义明确的通用术语存在,两个人可以用来指代同一组约束.如果假设本身是错误的,并且对 REST 的正式讨论毫无意义,请说出来.

Note 2:
Answer "REST does not have any constraints on that" would be a valid answer (which would mean that this approach is RESTful). Answers "it depends on who you ask" or "it is a best practice" is not really answering the question. The question assumes concept of REST exist as a well-defined common term two people can use to refer to the same set of constraints. If the assumption itself is incorrect and formal discussion of REST is meaningless, please do say so.

推荐答案

严格来说不是名词还是动词;这是关于你是否:

It's not strictly about nouns vs. verbs; it's about whether you are:

  • 识别资源
  • 通过表示操作资源

什么是资源?菲尔丁这样定义它:

What's a resource? Fielding defines it thusly:

REST 中信息的关键抽象是资源.任何可以命名的信息都可以是资源:文档或图像、时间服务(例如洛杉矶今天的天气")、其他资源的集合、非虚拟对象(例如人)等.换句话说,任何可能成为作者超文本引用目标的概念都必须符合资源的定义.资源是到一组实体的概念映射,而不是在任何特定时间点对应于映射的实体."

The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. a person), and so on. In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time."

现在,回答你的问题.你不能只看一个 URL 就说,这样一个 URL 从根本上与 REST 不兼容吗?"因为 REST 系统中的 URL 并不是真正重要的部分.更重要的是 URL process/123/pausecalculations/fibonacci 通过上述定义标识资源.如果他们这样做,则不会违反 REST 约束.如果他们不这样做,则您违反了 REST 的统一接口约束.你的例子让我相信它不符合资源定义,因此违反这个限制.

Now, to your question. You can't just look at a URL and say, "Is such-and-such a URL fundamentally incompatible with REST?" because URLs in a REST system aren't really the important bit. It's more important that the URLs process/123/pause and calculations/fibonacci identify resources by the above definition. If they do, there isn't a REST constraint violation. If they don't, you're violating the uniform interface constraint of REST. Your example leads me to believe it does not fit the resource definition and therefore would violate this constraint.

为了说明资源在这个系统中可能是什么,您可以通过将其发布到 paused-processes 资源集合来更改进程的状态.尽管这可能是处理流程的一种不同寻常的方式,但它从根本上与 REST 架构风格并不矛盾.

To illustrate what a resource might be in this system, you could change the status of a process by POSTing it to the paused-processes resource collection. Though that is perhaps an unusual way of working with processes, it's not fundamentally incompatible with the REST architecture style.

在计算的情况下,计算本身可能是资源,该资源可能如下所示:

In the case of calculations, the calculations themselves might be the resource and that resource might look like this:

Request:
GET /calculations/5

Response:
{
  fibonacci: 5,
  prime-number: true,
  square-root: 2.23607
}

尽管如此,这是一个有点不寻常的资源概念.我想稍微更典型的用法可能如下所示:

Though again, that's a somewhat unusual concept of a resource. I suppose a slightly more typical use might look like this:

Request:
GET /stored-calculations/12381728 (note that URL is a random identifier)

Response:
{
  number: 5,
  fibonacci: 5,
  prime-number: true,
  square-root: 2.23607
}

尽管您可能想要存储有关该资源的其他信息,而不是任何人都可以使用计算器进行的纯粹计算...

though presumably you'd want to store additional information about that resource other than a sheer calculation that anyone can do with a calculator...

Response:
{
  number: 5,
  fibonacci: 5,
  prime-number: true,
  square-root: 2.23607,
  last-accessed-date: 2013-10-28T00:00:00Z,
  number-of-retrievals-of-this-resource: 183
}

这篇关于在 URL 中使用动词从根本上与 REST 不兼容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆