如何RESTful地返回随机项目? [英] How to return random items RESTfully?

查看:86
本文介绍了如何RESTful地返回随机项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设计暴露了两种资源:

My design exposes two kinds of resources:

  1. 图片
  2. 标签

我希望客户能够通过其标签请求随机图像.例如:给我随机标记有"New York"和"Winter"的图像.在这种情况下,RESTful设计是什么样的?

I would like clients to be able to request random images by their tag(s). For example: Give me random images that are tagged with "New York" and "Winter". What would a RESTful design look like in this case?

推荐答案

总结所有评论中的讨论,而不是更改我最初的建议,这就是我最后要提出的内容:

To sum up all the discussion in the comments, and not to change my initial proposal, this is what I'd come up finally:

您想通过标签访问图像;每个标签与一组图像有关.由于给定标签的使用可能比其他标签更多(例如,纽约照片的使用量比芝加哥的更多),因此您应该使用允许缓存的RESTful配置,以便可以缓存纽约照片.恕我直言,解决方案将是:

You want to access images via tags; each tag relates to a set of images. As a given tag may be used a lot more than another (say, New York photos used a lot more than Chicago's), you should use a RESTful configuration that allows caching, so you can cache New York photos. IMHO, the solution would be:

  • 每个图像都有一个固定的URI:

  • Each image has a fixed URI:

http://www.example.com/images/12345

  • 每个标签还具有一个URI:

  • Each tag has also a URI:

    http://www.example.com/tags/New_York/random
    

    此URI充当集合中图像的随机分配器;它会返回 303 See Other 响应,并将其重定向到集的随机图像. 根据定义,该URI不得缓存,并且固定的应该是,浏览器不应该知道对第二个资源的重定向是永久的,因此这是最佳的.

    This URI acts as a random dispatcher of images on the set; it returns a 303 See Other response, redirecting to a random image of the set. By definition, this URI must not be cached, and the fixed one should, and the browser shouldn't understand that the redirection to the second resource is permanent, so it's optimal.

    您甚至可以通过以下方式访问整个集合:

    You could even access the whole set via:

    http://www.example.com/tags/New_York
    

    此访问权限将导致 300个多项选择回应;它将整个集合(作为URI,而不是图像!)返回给浏览器,然后浏览器决定如何处理它.

    This access would result in a 300 Multiple Choices response; it returns the whole set (as URIs, not as images!) to the browser, and the browser decides what to do with it.

    您还可以使用各种标签的交集:

    You can also use intersection of various tags:

    http://www.example.com/tags/New_York/Autumn/Manhattan/random
    http://www.example.com/tags/Autumn/Manhattan/New_York/random (equivalent to the previous one)
    http://www.example.com/tags/New_York/girls/Summer/random
    etc.
    

  • 因此,每个图像都有一个固定的URI,每个标签及其相关的照片集都有一个固定的URI,每个标签具有的随机调度程序有一个固定的URI.您不需要使用任何GET参数作为其他潜在的解决方案,因此这是您所能获得的RESTful.

    So you have a fixed URI for each image, a fixed URI for each tag and its related set of photos, and a fixed URI for a random dispatcher that each tag has. You haven't need to use any GET parameters as other potential solutions, so this is as RESTful as you can get.

    这篇关于如何RESTful地返回随机项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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