您将如何设计 RESTful 转换服务?

发布于 2024-11-30 03:19:47 字数 703 浏览 1 评论 0原文

我们正在创建一项服务来执行从一种格式到另一种格式的转换。转换的示例有货币、距离、时间、语言等。在我们的例子中,它是地理点(例如从十进制纬度/经度到度-分-秒纬度/经度)。

通常,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 技术交流群。

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

发布评论

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

评论(1

说谎友 2024-12-07 03:19:47

我建议使用参数和GET。我还会切换路径元素并将 /conversions 作为您的根资源,因为转换是您的“域核心”。

主要原因是:

  • 从 api-client 的角度来看,上面更容易使用(没有 POST 负载,非常容易测试/试用)。客户端友好性是 API 设计的最高优先级之一。
  • GET 更适合,因为您不需要“更改”服务器端的任何内容,而是转换内容。
GET /conversions/coordinate?input=xxx&format=yyy

我喜欢您使用 /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:

  • From api-client perspective above is easier to use (no POST payload, very easy to test/try-out). Client friendliness is one of the highest priorities for api-design.
  • GET fits better because you don't "change" anything on the server side but rather convert things.
GET /conversions/coordinate?input=xxx&format=yyy

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

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