问题描述
我已经写了支持标准CRUD操作一个RESTful Web服务,并能返回一组符合特定标准(一个搜索动词)的对象,但我想补充一个高阶COUNT动词,所以客户可以指望搜索标准匹配的资源,而无需获取所有的人。
I've written a RESTful web service that supports the standard CRUD operations, and that can return a set of objects matching certain criteria (a SEARCH verb), but I'd like to add a higher-order COUNT verb, so clients can count the resources matching search criteria without having to fetch all of them.
这是发生在我有几个选项:
A few options that occur to me:
-
忽略HTTP规范,并在HEAD请求的响应体返回对象计数。
Ignoring the HTTP specification and returning the object count in the response body of a HEAD request.
复制的搜索动词的逻辑,但做一个HEAD请求,而不是一个GET请求。然后,服务器将连接code在响应报头中的对象计数。
Duplicating the SEARCH verb's logic, but making a HEAD request instead of a GET request. The server then would encode the object count in a response header.
定义一个新的HTTP方法,伯爵,返回对象计数响应主体。
Defining a new HTTP method, COUNT, that returns the object count in the response body.
我preFER第一种方法的API,但我有罢工的选择,因为它是不符合规定的。第二种方法似乎是最语义正确,但是API不是很方便:客户端将要处理的响应头的时候,大部分的他们希望能够做一些简单的像 response.count时间
。所以我倾向于第三种方法,但我很担心涉及定义一个新的HTTP方法的潜在问题。
I'd prefer the API of the first approach, but I have to strike that option because it's non-compliant. The second approach seems most semantically correct, but the API isn't very convenient: clients will have to deal with response headers, when most of the time they want to be able to do something easy like response.count
. So I'm leaning toward the third approach, but I'm concerned about the potential problems involved with defining a new HTTP method.
你会怎么做?
推荐答案
剩下的主要目的是定义一组您用明确的动词互动资源。因此,你必须避免定义自己的动词。资源的数量应被视为一个不同的资源,拥有自己的URI,你可以简单地得到。
例如:
The main purpose of rest is to define a set of resources that you interact with using well defined verbs. You must thus avoid to define your own verbs. The number of resources should be considered as a different resource, with its own uri that you can simply GET.For example:
GET resources?crit1=val1&crit2=val2
返回资源的列表和
returns the list of resources and
GET resources/count?crit1=val1&crit2=val2
另一种选择是使用conneg:例如接受:文/ URI的列表
返回的资源列表和接受:纯文本/
仅返回计数
Another option is to use the conneg: e.g. Accept: text/uri-list
returns the resources list and Accept: text/plain
returns only the count
这篇关于我应该如何实现我的RESTful Web服务计数动词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!