REST接口查找平均 [英] REST interface for finding average
问题描述
假设我想创建一个REST接口找到号码列表的平均值。假设数字都是一次提交的。你会怎样做呢?
Suppose I want to create a REST interface to find the average of a list of numbers. Assume that the numbers are submitted one at a time. How would you do this?
- 发表数 http://site.com/api/average
- 如果这是一个哈希将返回第一号
- 发表数 http://site.com/api/average/hash
.... - http://site.com/api/average/hash 求平均值
- 删除 http://site.com/api/average/hash 因为我们不需要它更多的
- POST a number to http://site.com/api/average
- If this is the first number a hash will be returned
- POST a number to http://site.com/api/average/hash ....
- GET http://site.com/api/average/hash to find the average
- DELETE http://site.com/api/average/hash since we dont need it any more
这是做的正确方法?有什么建议么?
Is this the right way to do it? Any suggestions?
推荐答案
这更有意义,认为数字作为资源的名单。假设每个列表的资源URL是 /列表/ {ID}
,其中 {ID}
是列表的ID的占位符。然后:
It makes more sense to think of the list of numbers as the resource. Suppose each list's resource URL is /list/{id}
where {id}
is a placeholder for the list's ID. Then:
-
POST /列表
创建一个新的列表,服务器生成一个列表ID(或哈希),并指定了/列表/ {ID }
URL在响应中的位置
头。 -
POST /列表/ {ID}
添加一个数字列表 -
GET /列表/(编号)/平均
返回平均值 -
删除/列出/ {ID}
删除列表。
POST /list
creates a new list, the server generates a list ID (or 'hash') and specifies the/list/{id}
URL in the response'sLocation
header.POST /list/{id}
adds a number to the listGET /list/{id}/average
returns the averageDELETE /list/{id}
deletes the list.
到 GET /列表的替代/(编号)/平均
将是 GET /列表/ {ID}
返回列表作为结构化数据,如XML,其中包括的平均值作为一个生成的属性
An alternative to GET /list/{id}/average
would be for GET /list/{id}
to return the list as structured data, e.g. XML, that includes the average as a generated property.
这篇关于REST接口查找平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!