有关如何在仍符合REST原则的同时尽可能有效地处理集合的REST API问题 [英] REST API question on how to handle collections as effective as possible while still conforming to the REST principles

查看:120
本文介绍了有关如何在仍符合REST原则的同时尽可能有效地处理集合的REST API问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是REST的新手,但据我所知,我知道以下网址符合REST原则。资源的布局如下:

Im pretty new to REST but as far as i have gathered i understand that the following URL's conform to the REST principles. Where the resources are laid out as follows:

/user/<username>/library/book/<id>/tags
          ^         ^           ^   ^
          |---------|-----------|---|- user resource with username as a variable
                    |-----------|---|- many to one collection (books)
                                |---|- book id 
                                    |- many to one collection (tags)


GET /user/dave/library/book             //retrieves a list of books id's
GET /user/dave/library/book/1           //retrieves info on book id=1
GET /user/dave/library/book/1/tags      //retrieves tags collection (book id=1)

但是,如何优化此示例API呢?比如说我在我的图书馆里有10K书,我想要获取我图书馆里每本书的详细信息。我应该为 / library / book / library / book /< id> 进行http调用C>?或者我应该启用多个id作为参数? / library / book /< id1>,< id2> ... 并且确实喜欢一次使用100个ID进行批量提取?

However, how would one go about optimizing this example API? Say for example i have 10K books in my library and i want to fetch the details of every book in my library. should i really force a http call to /library/book/<id> for every id given in /library/book? Or should i enable multiple id's as parameters? /library/book/<id1>,<id2>... and do like bulk fetching with a 100 id's at a time?

REST原则对这种情况有何看法?你有什么看法?

What does the REST principles say about this kind of situation? and what are your opinion(s)?

再次感谢。

推荐答案

这严格来说就是设计问题。

This is strictly a design matter.

我可以定义一个 bookc 资源并像这样使用它: / p>

I could define a bookc resource and use it like this:

GET /user/dave/library/book?bookList=...

如何进一步指定 bookList 参数实际上是您设想的用法类型的问题这个资源。你可能有,例如:

how do you further specify the bookList argument is really a matter of what kind of usage you envisage of this resource. You could have, e.g.:

GET /user/dave/library/book?bookList=1-10
GET /user/dave/library/book?bookList=1,2,5,20-25

或你只需翻阅所有书籍:

or you could simply page through all of the books:

GET /user/dave/library/book?page=7&pagesize=50

但在我看来,特别是带有一长串随机ID的表格似乎很不合适。也许我会改为定义一个过滤器参数,所以我可以指定:

But in my mind, especially the form with a long list of "random" ids seems pretty unfit. Maybe I would instead define a filter parameter so I can specify:

GET /user/dave/library/book?filter=key,value&filter=key,value

至于您关于HTTP URL长度限制的问题,标准没有设置任何。但浏览器可能会有所不同......请看这个 S.O.主题

As to your question about HTTP URL length limit, the standard does not set any. But browser may vary... look at this S.O. topic

为了更严格地使用RESTful,可以通过HTTP标头指定查询参数,但我想传达的一般想法不会改变。

希望这似乎适合你...

Hope this seems suitable to you...

这篇关于有关如何在仍符合REST原则的同时尽可能有效地处理集合的REST API问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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