您将如何设计 RESTful 转换服务?
我们正在创建一项服务来执行从一种格式到另一种格式的转换。转换的示例有货币、距离、时间、语言等。在我们的例子中,它是地理点(例如从十进制纬度/经度到度-分-秒纬度/经度)。
通常,RESTful 资源具有类似的 OO 概念,该概念在服务器端持久存在,可以进行 CRUD(我知道这只是 RESTful 的一部分),但对于转换事物的简单服务来说,这并不适用。必然存在。如何让这一点变得 RESTful 呢?目前我们有这样的一些:
要获取支持的格式列表,可以这样做:
GET /coordinates/formats
这将返回格式列表,例如包括 DD 和 DMS(十进制度和度-分-秒)。
然后,我们可以转身并执行如下转换:
POST /coordinates/DD/as/DMS
在本例中,我们将在请求正文中传递十进制度的表示形式(作为 JSON 或某种其他格式),并在响应正文中接收度-分-秒的表示形式。
这当然可行,但感觉不太好,特别是因为相同的 URI 反复用于不同的输入(不同的十进制)。诀窍可能是真正集中精力于正在操纵它的资源。也许它是一个“转换”:
POST /coordinate/conversions
主体可能会接受值、其格式和所需的输出。然而,所有资源的 URI 仍然相同...
有什么想法吗?
We are creating a service to perform conversions from one format to another. Examples of conversions are currencies, distances, times, languages, etc. In our case it is geographic points (for example from decimal degrees latitude/longitude to degrees-minutes-seconds latitude/longitude).
Typically a RESTful resource has an analogous OO concept that is persistent on the server side that can be CRUDed (I know this is only part of what makes something RESTful), but in the case of a simple service that converts things, that doesn't necessarily exist. How would one make this RESTful? Currently we have some like this:
To get the list of supported formats one can do this:
GET /coordinates/formats
Which would return a list of formats, including, for example DD and DMS (decimal degrees and degrees-minutes-seconds).
One can then turn around and perform a conversion like so:
POST /coordinates/DD/as/DMS
In this example one would pass a representation of decimal degrees (as JSON or some other format) in the request body, and receive a representation of degrees-minutes-seconds in the response body.
This of course works, but feels unRESTful, in particular because the same URI is used over and over for different inputs (different decimal degrees). The trick is probably to really concentrate on what the resource being manipulated it. Perhaps its a "Conversion":
POST /coordinate/conversions
The body might take in the a value, its format, and the desired output. However, the URI is still the same for all resources...
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用参数和
GET
。我还会切换路径元素并将/conversions
作为您的根资源,因为转换是您的“域核心”。主要原因是:
我喜欢您使用
/conversions/formats
返回元数据的方法。它使格式易于查找,并且您的 api 更易于解释。然后,相应的数据形成上述调用中“格式”参数的可能值。或者您是否正在存储转换(这将有利于状态更改 POST 方法)?
I would suggest using parameters and
GET
. I also would switch path elements and make/conversions
your root-resource, as conversion is your "domain-core".Main reasons are:
I like your approach to give back metadata with
/conversions/formats
. It makes formats easy to lookup and your api more self-explainable. The respective data then forms the possible value for 'format'-parameter from above call.Or are you storing conversion (which would favor state-changing POST method)?