你能构建一个真正的 RESTful 服务,它需要很多参数吗? [英] Can you build a truly RESTful service that takes many parameters?

查看:22
本文介绍了你能构建一个真正的 RESTful 服务,它需要很多参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读一篇关于 REST 的文章("Restful Grails"),我的印象是,在需要大量参数的服务中不可能真正符合 REST 风格.是这样吗?到目前为止,我看到的所有示例似乎都暗示真正的 REST 风格服务是无参数的".使用参数将是 RPC-ish 而不是真正的 RESTful.

After reading an article on REST ("Restful Grails"), I have gotten the impression that it is not possible to truly conform to a REST style in a service that demands a lot of parameters. Is this so? All the examples I have seen so far seem to imply that true REST style services are "parameterless". Using parameters would be RPC-ish and not truly RESTful.

更具体地说,假设我们有一个返回股票价格图表数据的服务,该服务需要知道开始日期、结束日期、货币、股票名称以及其他任何可能适用的信息.在任何情况下,至少需要 4-5 个参数来检索所需的信息.我会想象 URL 是这样的:/stocks/YAHOO?startDate="2008-09-01"&endDate=...(雅虎"在这里是一个虚构的股票名称).

To be more specific, say we have a service that returns graph data for stock prices, and this service needs to know the start date, end date, the currency, stock name, and whatever else might be applicable. In any case, at least 4-5 parameters are needed to retrieve the information needed. I would imagine the URL to be something like this : /stocks/YAHOO?startDate="2008-09-01"&endDate=... ("YAHOO" is here a made-up stock name).

这真的是 REST 还是更像 RPC,上述文章的作者称之为GETful"(即只是低仪式 rpc)?

Would this really be REST or is this more RPC-like, what the author of the aforementioned article calls "GETful" (i.e. just low ceremony rpc)?

推荐答案

您可以将查询字符串视为您正在获取的资源的过滤器.在这里,您的资源是雅虎的股票价格.对该资源执行 GET 为您提供所有可用数据或最新数据.查询字符串过滤您想要的价格.内容协商允许您更改表示,例如png 图形、csv 文件等.要添加价格,只需将表示(例如 CSV)发布到同一资源.

You can see the querystring as a filter on the resource you are GETing. Here, your resource is the stock prices of yahoo. Doing a GET on that resource give you all the available data, or the most recents. The query string filter the prices you want. Content negociation allow you to change the representation, e.g. a png graph, a csv file, and so on. To add a price, simply POST a representation (e.g. CSV) to the same resource.

宁静"并不真正存在于 URL 本身,因为 URI 对客户端来说是模糊的,而是在于您与由其 URI 标识的资源本身进行交互的方式

The "restfulness" is not realy in the URL itself, since URIs are obscures to client, but in the way you interact with resources themselves identified by their URI

这篇关于你能构建一个真正的 RESTful 服务,它需要很多参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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