哪些映射用于 GET 请求? [英] Which mappings to use for GET requests?

查看:58
本文介绍了哪些映射用于 GET 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于应该代表我的 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} - 获取给定 titleauthor 的所有书籍/li>

    • GET /book - get all books
    • GET /book/{id} - get one book with specific id
    • GET /book?title={title}&author={author} - get all books with given title and author
    • 我预测,你可以有更多的搜索选项,所以我更喜欢它的另一个端点:

      I predict, that you could have more options for search, so I would prefer another endpoint for it:

      • 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 use GET here, because GET request could be cashed by any part of network within alive period).

      注意:在我看来titleauthor 不是域对象,因此我不认为使用像 GET/book/author/{author}GET/book/title/{title} 这样的端点.但是我不介意为一本书使用端点,例如:

      Note: in my opinion, title or author is not a domain object and therefore I do not use endpoints like GET /book/author/{author} or GET /book/title/{title}. BUT I do not mind to use endpoints for one single book, like:

      • GET/book/{id}/author - 获取给定书籍的所有作者
      • GET/book/{id}/title - 获取给定书的标题
      • GET /book/{id}/author - get all authors for given book
      • GET /book/{id}/title - get title for given book

      我希望我已经回答了你的问题.

      I hope I have answered you question.

      这篇关于哪些映射用于 GET 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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