Spring Data REST:如何在一次调用中使用 Id 列表检索多个项目? [英] Spring Data REST: How to retrieve many items using list of Ids in one single call?
本文介绍了Spring Data REST:如何在一次调用中使用 Id 列表检索多个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过如下调用从 Spring Data REST 中检索一本书:获取/book/{id}
I can retrieve one single book from Spring Data REST with a call such as: GET /book/{id}
现在,如果我知道两本书的 ID 并且我想一次检索它们?应该叫什么?我尝试了以下操作,但它返回的书籍与指定的书籍不同:
Now, if I know the Ids of two books and I want to retrieve them all at once? What should the call be? I tried the following but it is returning me different books than the specified ones:
GET /book?ids=id1,id2
推荐答案
你可以像这样在你的 Repository 接口中声明一个查询方法:
You could declare a query method in your Repository interface like this:
List<Book> findByIdIn(@Param("ids") Long[] ids);
这样您就可以通过这种方式请求图书:
So that you can request books this way:
GET /book/search/findByIdIn?ids=1,6,9
这篇关于Spring Data REST:如何在一次调用中使用 Id 列表检索多个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文