简体   繁体   English

总是通过ID来休息资源吗?

[英]Rest Resource always by Id?

In a RESTfull API, what is the best way to represent a filtering of a resource? 在RESTfull API中,代表资源过滤的最佳方法是什么? Most examples speak of an URL that is structured /resouce/9/subresource/5 . 大多数示例都谈到了结构化/resouce/9/subresource/5的URL。 I wish to represent a filtered list of a resource like this: /resource/9/subresource/validated . 我希望代表这样的资源的过滤列表: /resource/9/subresource/validated This validated property is a top-level concept in the domain of the API. 这个validated属性是API领域中的顶级概念。

Is this considered 'okay' in a restfull api? 在restfull api中这被认为“还可以”吗? Moving the validated to the querystring feels like it diminishes the importance of the concept. 将已验证的内容移至查询字符串感觉就像它削弱了该概念的重要性。

Edit 编辑

Let me try to explain my question with a concrete example: The api of stackexchange has the following resource: /users/{id}/notifications/unread . 让我尝试用一​​个具体的例子来解释我的问题: stackexchange的api具有以下资源: /users/{id}/notifications/unread One could argue that the list of unread notifications can be considered a resource on its own. 有人可能会认为,未读通知列表本身可以被视为一种资源。

If you want a purist REST API, remember that each resource type is represented by the same url , so something like /resouce/9/subresource/5 should return something very different than /resource/9/subresource/validated . 如果您想要一个纯粹的REST API,请记住, 每种资源类型都由相同的url表示 ,因此类似/resouce/9/subresource/5东西应该返回与/resource/9/subresource/validated不同的东西。

Most APIs I know use query parameters to get a subset of the resources (for example, for filtering, pagination, etc). 我知道的大多数API都使用查询参数来获取资源的子集(例如,用于过滤,分页等)。 In you case the urls would be something like /resource/9/subresource?validated=true 在您的情况下,URL类似于/resource/9/subresource?validated=true

Some APIs follow a different approach and had specific urls that work as filters (as your /validated example). 某些API采用不同的方法,并具有充当过滤器的特定网址(如您的/validated示例)。 The reason behind this is usually that there's a perception that there could be quite a lot of complexity on the API... which is the case if it's not designed properly. 其背后的原因通常是人们认为API可能存在很多复杂性……如果设计不当,就是这种情况。 Imagine in the case that some parameters only work when other parameters are set, for example that pagination works if you query the whole collection, but not if you want only the validated resources, mixed with poor documentation) 想象一下,如果某些参数仅在设置了其他参数时才起作用,例如,如果您查询整个集合,则分页才起作用,但是如果您只想要validated资源,并且文档质量很差,那么分页就无法工作)

About moving the validated to the query string.. can you clarify why it would diminish the concept of validated ? 关于将经过validated移至查询字符串..您能否阐明为什么它会减少经过验证的概念? As on REST API the resource is king, rather than a property of the resource. 与在REST API上一样,资源为王,而不是资源的属性。

(note: I have no idea how you use backslashes on your urls) (注意:我不知道您如何在网址上使用反斜杠)

Edit 编辑

Trying to answer your comment as I think this is important! 尝试回答您的评论,因为我认为这很重要! :). :)。

If those URLs return a list that contain the same resource, let's say a user , then validated, removed, foobared, etc are states of the user, while user is the resource. 如果这些URL返回包含相同资源的列表,则假设user ,然后经过验证,删除,foobared等是该用户的状态 ,而user 该资源。

I had similar doubts when I started building REST APIs, and it does require mind shift. 当我开始构建REST API时,我也有类似的疑问,这确实需要转变。 The URLs have to represent resources, and not state or operations/commands. URL必须代表资源,而不是状态或操作/命令。

Edit 2 编辑2

Ok, that explains the confusion. 好的,这解释了混乱。 The Stack Exchange API is ... not really a REST API (the word REST is not even mentioned on that page). Stack Exchange API并不是真正的REST API(在该页面上甚至没有提到REST一词)。 It's more a RPC over HTTP, which uses JSON as the representation. 它是一个基于HTTP的RPC,它使用JSON作为表示。

To clarify the above statement (and before I get kicked out of the site ;). 为了澄清上述说法(以及在我被赶出网站之前;)。 REST is usually explained as having 4 levels, which you can read on this blog post Haters gonna HATEOAS . REST通常被解释为具有4个级别,您可以在此博客文章Haters gonna HATEOAS上阅读 I'm copying here the one of the relevant bits regarding the levels of conformity to REST. 我在这里复制有关REST一致性级别的相关信息之一。

  1. “The Swamp of POX.” You're using HTTP to make RPC calls. “ POX的沼泽”。您正在使用HTTP进行RPC调用。 HTTP is only really used as a tunnel. HTTP仅真正用作隧道。
  2. Resources. 资源。 Rather than making every call to a service endpoint, you have multiple endpoints that are used to represent resources, and you're talking to them. 有多个端点用于表示资源,而不是对服务端点进行每次调用,而您正在与它们交谈。 This is the very beginnings of supporting REST. 这是支持REST的开始。
  3. HTTP Verbs. HTTP动词。 This is the level that something like Rails gives you out of the box: You interact with these Resources using HTTP verbs, rather than always using POST. 这是Rails这样的开箱即用的级别:您可以使用HTTP动词而不是始终使用POST与这些资源进行交互。
  4. Hypermedia Controls. 超媒体控件。 HATEOAS. HATEOAS。 You're 100% REST compliant. 您100%符合REST。

The StackExchange API is at most at level 2 (level 1 would be 1 endpoint using something like SOAP). StackExchange API的最高级别为2(使用SOAP之类,级别1将是1个终结点)。 Personally, I only recognise an API as 'REST' if it uses at least HTTP verbs, otherwise it is still RPC over HTTP, but with the only distinction that you have several endpoints. 就我个人而言,我仅将API至少使用HTTP动词识别为“ REST”,否则它仍然是RPC over HTTP,但唯一的区别是您有多个端点。

So maybe the question for you is: What level of REST do you want to implement in your API? 因此,也许您的问题是:您想在API中实现什么级别的REST? A word of caution here, going full HATEOAS might sound really cool, but it will increase the complexity of your application... so you have to ask yourself if you're ok with paying for that extra complexity (eg autodiscovery of new operations). 请注意,全面安装HATEOAS听起来确实很酷,但这会增加应用程序的复杂性……因此,您必须问自己是否可以为这种额外的复杂性付钱(例如,自动发现新操作) 。

And really important, build the API that you think will be the easiest to use for your clients. 真正重要的是,构建您认为最容易为客户使用的API。 It's better to take a practical approach to REST rather than a religious one, and as an example, here are the US White House REST standards . 最好采用一种实用的REST方法,而不是一种虔诚的方法,例如,以下是美国白宫REST标准 Give that a read, I really agree on most, if not all, of the practices in there. 仔细阅读一下,我真的同意那里的大多数(如果不是全部)实践。

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

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