哪些映射用于 GET 请求? [英] Which mappings to use for GET requests?
问题描述
我有一个关于应该代表我的 RESTful 应用程序的 URI 的问题.我已经阅读了许多关于不同资源的主题,但我仍然不确定应该如何实现以下情况:
I have a question about URI that should represent my RESTful application. I've read many topics on different resources but I still unsure how should I implement the following situation:
我的控制器中有 3 个方法:
I have 3 methods in my controller:
List<Books> getAllBooks()
List<Books> getBooksByTitle(String title)
List<Books> getBooksByAuthor(String author)
很明显,我应该为第一种方法选择 "/books"
URI 但如果我映射 "/books/title?"
或 "/books/author?"
对于下一个方法,抛出异常模糊映射"."/books/title/"
和 "/books/author/"
似乎不是一个好的选择.
It's obvious that I should choose "/books"
URI for the 1st method but if I map "/books/title?"
or "/books/author?"
for next methods an exception "Ambiguous mapping" is throwed. "/books/title/"
and "/books/author/"
doesn't seem to be a good choice.
我可以建议什么解决方案?
What solution can I advice me?
推荐答案
GET/book
- 获取所有书籍GET/book/{id}
- 获取一本具有特定id
的书GET/book?title={title}&author={author}
- 获取给定title
和author
的所有书籍/li>GET /book
- get all booksGET /book/{id}
- get one book with specificid
GET /book?title={title}&author={author}
- get all books with giventitle
andauthor
POST/book/search
BODY: {"author": "aaa", "title": "bbb"} - 根据给定的搜索请求获取所有书籍(这里使用body,所以这里不能使用GET
,因为GET
请求可以在存活期内被网络的任何部分兑现).POST /book/search
BODY: {"author": "aaa", "title": "bbb"} - get all books according to given search request (we use body here, so we could not useGET
here, becauseGET
request could be cashed by any part of network within alive period).GET/book/{id}/author
- 获取给定书籍的所有作者GET/book/{id}/title
- 获取给定书的标题GET /book/{id}/author
- get all authors for given bookGET /book/{id}/title
- get title for given book
我预测,你可以有更多的搜索选项,所以我更喜欢它的另一个端点:
I predict, that you could have more options for search, so I would prefer another endpoint for it:
注意:在我看来,
title
或author
不是域对象,因此我不认为使用像GET/book/author/{author}
或GET/book/title/{title}
这样的端点.但是我不介意为一本书使用端点,例如:Note: in my opinion,
title
orauthor
is not a domain object and therefore I do not use endpoints likeGET /book/author/{author}
orGET /book/title/{title}
. BUT I do not mind to use endpoints for one single book, like:我希望我已经回答了你的问题.
I hope I have answered you question.
这篇关于哪些映射用于 GET 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!