设计 RESTful API 时如何正确公开布尔搜索函数?

发布于 2024-12-20 12:18:53 字数 326 浏览 4 评论 0原文

我正在为搜索功能设计一个 RESTful API,如下所示:

http://example.com/books/?author=John+Smith&title=Brain+Surgery+For+Dummies

看着这个 URI,我不能立即知道客户端正在执行哪两个查询:

a) John Smith 的所有书籍,以及所有题为“Brain Surgery for Dummies”的书籍

b) 所有版本的“Brain Surgery for Dummies”作者:约翰·史密斯。

我如何重新设计 URI 以使其更加明确?

I'm designing a RESTful API for a search function that looks something like this:

http://example.com/books/?author=John+Smith&title=Brain+Surgery+For+Dummies

Looking at this URI, I don't immediately know which these two queries the client is performing:

a) all books by John Smith, plus all books entitled "Brain Surgery for Dummies"

b) all editions of "Brain Surgery for Dummies" authored by John Smith.

How might I redesign my URIs to make this more explicit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

秉烛思 2024-12-27 12:18:53

据我从示例中了解到,“作者”是一种资源:

http://example.com/books/authors/John+Smith

http://example.com/books/authors/john-smith

在作者的图书集中搜索图书

http://example.com/books/authors/John+Smith?title=Brain+Surgery+For+Dummies

http://example.com/books/authors/john-smith?title=Brain+Surgery+For+Dummies

在图书集中搜索图书:

http://example.com/books?title=Brain+Surgery+For+Dummies

在任何一种情况下搜索都意味着该书的确切名称/标题未知,并且搜索可能会返回多个物品,尽管特定的书本身也是一种资源。在这种情况下,您需要为书籍定义 URI:

书籍资源:

http://example.com/books/brain-surgery-for-dummies

As I understand from examples "author" is a resource:

http://example.com/books/authors/John+Smith

OR

http://example.com/books/authors/john-smith

Search book in the author's books collection

http://example.com/books/authors/John+Smith?title=Brain+Surgery+For+Dummies

OR

http://example.com/books/authors/john-smith?title=Brain+Surgery+For+Dummies

Search book in the books collection:

http://example.com/books?title=Brain+Surgery+For+Dummies

Searching in either case means that exact name/title of the book is not known and search can possibly return multiple items, though particular book by itself is a resource too. In this case you need to define URI for book:

Book resource:

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