简体   繁体   English

这是RESTful吗?

[英]Is this RESTful?

I have a Rails app that needs to expose values from a database as a web service - since I'm using Rails 2.x, I'm going with REST (or at least try). 我有一个Rails应用程序需要将数据库中的值作为Web服务公开 - 因为我正在使用Rails 2.x,我将使用REST(或至少尝试)。 Assuming my resource is Bananas, for which I want to expose several sub-characteristics, consider this: 假设我的资源是Bananas,我想为其揭示几个子特征,请考虑以下因素:

 - /banana          -> give a summary of the first 10 bananas, in full (all characteristics)
 - /banana/?name=<name>         -> give all characteristics for banana named <name>
 - /banana/?number=<number>         -> give all characteristics for banana number <number>
 - /banana/?name=<name>/peel          -> give peel data for banana named <name>
 - /banana/?number=<number>/length         -> give length data for banana number <number>

I don't want to search for ID, only name or number . 希望搜索ID,只有名称编号 And I have about 7 sub-characteristics to expose. 我有大约7个子特征要揭露。 Is this RESTful? 这是RESTful吗?

Thanks for any feedback! 感谢您的任何反馈!

What Wahnfrieden is talking about is something called Hypermedia as the Engine of Application State (HATEOAS) - a central constraint of REST as defined by Fielding. Wahnfrieden所谈论的是称为超媒体作为应用程序状态引擎 (HATEOAS)的东西 - 由Fielding定义的REST的核心约束。

In a nutshell, REST application clients never construct URIs themselves. 简而言之,REST应用程序客户端永远不会自己构造URI。 Instead, they follow URIs provided by the application. 相反,它们遵循应用程序提供的URI。 So, URI templates such as the ones you're asking about are irrelevent at best. 因此,诸如您所询问的URI模板充其量只是无关紧要的。 You can make them conform to a system if you'd like, but REST says nothing about how your URIs need to look. 如果您愿意,可以使它们符合系统,但REST没有说明您的URI需要如何查看。 You could, if you wanted to, arrange it so that every resource in your system was available from http://example.com/ {hash}. 如果您愿意,可以安排它,以便系统中的每个资源都可以从http://example.com/ {hash}获得。

Publishing URI templates, such as the ones you're talking about in your question, introduces tight coupling between your application and clients - something REST is trying to prevent. 发布URI模板(例如您在问题中讨论的模板)会引入应用程序和客户端之间的紧密耦合 - 这是REST试图阻止的。

The problem with understanding hypermedia-driven applications is that almost nobody implements or documents their "RESTful" systems this way. 理解超媒体驱动的应用程序的问题在于,几乎没有人以这种方式实现或记录其“RESTful”系统。

It might help to think about the interaction between a human and server via a browser. 通过浏览器考虑人与服务器之间的交互可能会有所帮助。 The human only knows about content and links that the server provides through the browser. 人类只知道服务器通过浏览器提供的内容和链接。 This is how a RESTful system should be built. 这就是构建RESTful系统的方式。 If your resources aren't exposing links, they're probably not RESTful. 如果您的资源没有公开链接,那么它们可能不是RESTful。

The advantage is that if you want to change your URI system, for example, to expose the Banana "Peel" attribute through a query parameter instead of a nested URL, you can do it anytime you'd like and no client code needs to be changed because they're not constructing links for themselves. 优点是,如果要更改URI系统,例如,通过查询参数而不是嵌套URL公开Banana“Peel”属性,您可以随时执行此操作,并且不需要客户端代码因为他们没有为自己构建链接而改变了。

For an example of a system that embraces the hypertext-driven constraint in REST, check out the Sun Cloud API . 有关在REST中包含超文本驱动约束的系统示例,请查看Sun Cloud API

I would use these: 我会用这些:

  • /banana /香蕉
  • /banana/blah /香蕉/嗒嗒
  • /banana/123 /香蕉/ 123
  • /banana/blah/peel (and /banana/123/peel) / banana / blah / peel(和/ banana / 123 / peel)
  • /banana/blah/length (and /banana/123/length) / banana / blah / length(和/ banana / 123 / length)

First, common practice for ReSTful URIs is /object_name/id/verb, with some of those absent (but in that order). 首先,ReSTful URI的常见做法是/ object_name / id / verb,其中一些缺席(但按此顺序)。 Of course, this is neither required nor expected. 当然,这既不是必需的,也不是预期的。

If all your names aren't made of digits, you don't have to explicitly have name in /banana/name/blah. 如果您的所有名称都不是数字,则不必在/ banana / name / blah中明确name名称。 In fact, if anything, it would be better to have id as identifier: /banana/id/123/peel. 事实上,如果有的话,最好将id作为标识符:/ banana / id / 123 / peel。 Hope this helps. 希望这可以帮助。

Parameters should only be used for form submission. 参数只应用于表单提交。

Also, URI naming schemas is totally unrelated to REST. 此外,URI命名模式与REST完全无关。 The point of REST is to make related resources discoverable via hypertext, not out-of-band conventions, and only from a limit number of entry points. REST的目的是通过超文本,而不是带外约定,并且仅从极限数量的入口点使相关资源可被发现。 So your /bananas/ entry point might provide the summary info for 10 bananas, but it must also provide the URI for each of those bananas' details resources, as well as the URI to get the summary for the next 10 bananas. 因此,您的/ bananas /入口点可能会提供10个香蕉的摘要信息,但它还必须为每个香蕉的详细信息资源提供URI,以及获取下10个香蕉的摘要的URI。 Anything else is just RPC. 其他任何东西都只是RPC。

It is good practice in REST to not use query parameters because query parameters don´t belong to a URL and in REST all resources should be addressable through a URL. REST中的优良做法是不使用查询参数,因为查询参数不属于URL,而在REST中,所有资源都应通过URL进行寻址。

In your example /banana/?name= name should be /banana/ name because you are referring a concrete resource. 在您的示例中/ banana /?name = name应为/ banana / name,因为您指的是具体资源。

Even I think /banana/?number= number /length is not good REST style, because you are selecting an attribute through a URL when you should retrieve the whole state with /banana/ name . 即使我认为/ banana /?number = number / length不是很好的REST风格,因为当你应该使用/ banana / name检索整个状态时,你是通过URL选择一个属性。 A difference could be /customers/1024/address to get the Customer 1024 address record. 可以使用/ customers / 1024 / address来获取Customer 1024地址记录。

HTH. HTH。

A more opt form for the route in url having query string is the plural form, as it is possible that multiple items are returned in the result. 具有查询字符串的url中的路径的更多选择形式是复数形式,因为可能在结果中返回多个项目。 In this case, bananas, like bananas?color=yellow, sounds more appropriate. 在这种情况下,香蕉,如香蕉?颜色=黄色,听起来更合适。 On the other hand, the singular form banana, like banana/123, is good when fetching a specific resource's representation when its identifier is known and query string is not required. 另一方面,单个形式的香蕉,如香蕉/ 123,在获取特定资源的表示时,当其标识符已知且不需要查询字符串时,是很好的。

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

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