如何在 Restful Web 服务中指定一系列数据或多个实体

发布于 2024-12-02 06:37:14 字数 211 浏览 0 评论 0原文

要访问 Restful Web 服务中的用户实例,URL 的结构如下面的curl 请求所示:

curl -v -X GET -s  "$BASE_URL/User/${customer_id}.json"

如果我想指定所有用户实体或通过一系列用户实体进行分页,例如我的前 50 个用户数据库,我将如何构建我的请求以使其符合 REST ???

To access an instance of a User in a restful web-service the url is structured as shown in the curl request below:

curl -v -X GET -s  "$BASE_URL/User/${customer_id}.json"

If I wanted to specify all User entities or page through a range of User entities, such as the first 50 Users in my database, how would I structure my request so that it is compliant with REST ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2024-12-09 06:37:14

您应该首先尝试弱化 URI 中字符的含义。虽然漂亮、漂亮和可读的 URI 是一件好事,但它们与 REST 无关——事实上,通过将所有 URI 更改为无意义的字符串,然后添加漂亮的字符串来判断 RESTful 接口的设计是一个很好的练习。客户端永远不应该知道服务器的 URI 结构,原因很简单,服务器之后将无法更改它。

接下来,如果客户列表对您来说是一个有意义的概念,您应该尽一切努力将其转变为自己的资源。在其表示中,您可能希望包含有关每个客户的一些信息,以及指向每个客户资源的链接。

寻呼?好主意——将每个页面变成自己的资源,并将它们链接在一起。看一下 Google 呈现搜索结果的方式以获取提示。

通常这就是您所需要的,因为客户端不需要能够指定页面大小。但如果是这样,您基本上有两个选择:您可以拥有一个封装表单的资源,该表单允许您指定参数,这些参数在提交时将通过重定向直接或间接将您带到适当的 URI。或者您可以包含一个穷人的表单,一个 URI 模板。

无论如何,重申我的第一点:不要将 RESTful 接口视为将自身表现为一组 URI 构造规则的 API。相反,请考虑资源、表示和超媒体。

You should start by trying to de-emphasize the meaning of the characters in a URI. While nice, pretty and readable URIs are a good thing, they have nothing to do with REST -- in fact it's a good exercise to judge the design of a RESTful interface by changing all the URIs to meaningless strings, and add the prettiness afterwards. A client should never have any knowledge of the server's URI structure, for the simple reason that the server won't be able to change it afterwards.

Next, if a list of customers is a meaningful concept to you, you should by all means turn it into a resource of its own right. In its representation, you might want to include some information about each individual customer, along with a link to each individual customer resource.

Paging? Good idea -- turn each page into its own resource, and link them together. Take a look at the way Google presents search results for a hint.

Often that's all you need because it's not necessary for the client to be able to specify the page size. But if it is, you essentially have two options: You can have a resource that encapsulates a form that allows you to specify the parameters that, when submitted, will directly or indirectly via a redirect take you to the appropriate URI. Or you can include a poor man's form, a URI template.

In any case, and to re-iterate my first point: Don't view a RESTful interface as an API that manifests itself as a set of URI construction rules. Instead, think of resources, representations, and hypermedia.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文