通过示例了解 REST [英] Understanding REST through an example

查看:34
本文介绍了通过示例了解 REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我唯一真正接触到 REST 的想法是通过 Ruby on Rails 的 RESTful 路由.这非常适合我使用 Rails 构建的那种基于 CRUD 的应用程序,但因此我对 RESTfulness 的理解有些有限.

My only real exposure to the ideas of REST has been through Ruby on Rails' RESTful routing. This has suited me well for the kind of CRUD-based applications I have built with Rails, but consequently my understanding of RESTfulness is somewhat limited.

假设我们有一个有限的项目集合,每个项目都有一个唯一的 ID,以及许多属性,例如颜色、形状和大小(对于某些项目可能未定义).

Let's say we have a finite collection of Items, each of which has a unique ID, and a number of properties, such as colour, shape, and size (which might be undefined for some Items).

物品可以被一个客户使用一段时间,但每个物品一次只能被一个客户使用.对物品的访问由服务器管理.客户端可以向服务器请求临时使用某些项目.

Items can be used by a client for a period of time, but each Item can only be used by one client at once. Access to Items is regulated by a server. Clients can request the temporary use of certain items from a server.

通常,客户只对访问具有特定属性的多个项目感兴趣,而不是访问特定项目.

Usually, clients will only be interested in getting access to a number of Items with particular properties, rather than getting access to specific Items.

当客户端请求使用多个 Item 时,服务器会使用与该请求对应的 ID 列表进行响应,或者响应表明所请求的 Item 当前不可用或不存在.

When a client requests use of a number of Items, the server responds with a list of IDs corresponding to the request, or with a response that says that the requested Items are not currently available or do not exist.

客户端可以发出以下类型的请求:

A client can make the following kinds of request:

  • 告诉我有多少个绿色三角形物品(总共/可用).
  • 给我使用 200 个大红色物品.
  • 我已经完成了第 21、23、23 项.
  • 添加 100 个新的红色方块项目.
  • 删除 50 个绿色小项目.
  • 将所有大黄色五边形项目修改为蓝色.

上面的玩具示例就像我最近不得不处理的资源分配问题.我应该如何以 REST 的方式考虑它?

The toy example above is like a resource allocation problem I have had to deal with recently. How should I go about thinking about it RESTfully?

推荐答案

理解的诀窍是从名词而不是动词的角度来思考问题.

The trick to understanding is to think about the problems from a focus on nouns instead of verbs.

在其他世界,动词都是预设"的,名词变得无限灵活.在肥皂或 roc 世界中,动词是无限灵活的.将您的思维限制在锁定动词上,然后在您的限制范围内查看您需要使用哪些名词来解决您的问题.

In the rest world, the verbs are all "preset" and the nouns become infinitely flexible. In a soap or roc world, the verbs are infinitely flexible. Restrict your thinking to locking down the verbs and then see what nouns you need to solve your problem within the constraints you have.

这正是 darrel 在上面的答案中所做的 - 他为锁创建了一个新名词以满足您的约束,然后设置对它们的访问以实现您想要的.

That is exactly what darrel did in the answer above - he made up a new noun for a lock that would satisfy your constraints and then set up access to them to achieve what you wanted.

您的一些问题与搜索或过滤器相关 - 对于那些认为 GET 针对资源类型、传入查询参数以限制或过滤结果的人.

Some of your questions were search or filter related - for those think of GET against the types of resources, passing in query parameters to restrict or filter the results.

这篇关于通过示例了解 REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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