如何在宁静的Web服务中指定数据范围或多个实体 [英] how to specify a range of data or multiple entities in a restful web-service

查看:77
本文介绍了如何在宁静的Web服务中指定数据范围或多个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要访问静态Web服务中的User实例,URL的结构如下面的curl请求所示:

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"

如果我想指定所有用户实体或整个用户实体范围内的页面,例如数据库中的前50个用户,我将如何构造我的请求以使其与REST兼容?

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 ???

推荐答案

您应该首先尝试不强调URI中字符的含义.虽然漂亮,漂亮且可读的URI是一件好事,但它们与REST无关-实际上,通过将所有URI更改为无意义的字符串来判断RESTful接口的设计是一个不错的练习,然后再添加漂亮性.客户端永远不应该了解服务器的URI结构,原因很简单,那就是服务器之后将无法对其进行更改.

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.

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

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.

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

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.

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

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.

这篇关于如何在宁静的Web服务中指定数据范围或多个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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