java 在发布请求中传递可分页(弹簧数据)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34889613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing pageable (spring data) in post request
提问by Raja Krishnan
I have a rest API server which has the following API.
I have some other APIs, where i get pageable from GET
requests. Here, I need to make a post request for passing the queryDto. So, I cannot pass the page=0?size=20
etc as url parameters.
我有一个具有以下 API 的休息 API 服务器。我有一些其他的 API,我可以从GET
请求中获得分页。在这里,我需要发出一个 post 请求来传递 queryDto。所以,我不能将page=0?size=20
等作为 url 参数传递 。
I would like to know how to pass pageable as JSON object to a POST
request
我想知道如何将 pageable 作为 JSON 对象传递给POST
请求
@RequestMapping(value = "/internal/search", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ResponseList<Object> findObjects(@RequestBody QueryDto queryDto, Pageable pageable) {
if (queryDto.isEmpty()) {
throw new BadRequestException();
}
return someService.findObjectsByQuery(queryDto, pageable);
}
回答by Lucas Oliveira
I Think that is not possible, at least not already provided by the framework.
我认为这是不可能的,至少框架还没有提供。
The Spring has a HandlerMethodArgumentResolver
interface with an implementation called PageableHandlerMethodArgumentResolver
that retrieves the request param value calling something like HttpServletRequest.getParameter. So, you can bind the Pageable instance passing the parameters "page" and "size" for GET and POST. So, the following code works:
Spring 有一个HandlerMethodArgumentResolver
接口,其中有一个调用实现调用PageableHandlerMethodArgumentResolver
HttpServletRequest.getParameter 来检索请求参数值。因此,您可以绑定 Pageable 实例,为 GET 和 POST 传递参数“page”和“size”。因此,以下代码有效:
@RequestMapping(value="/test",method = RequestMethod.POST)
@ResponseBody
public String bindPage(Pageable page){
return page.toString();
}
$ curl -X POST --data "page=10&size=50" http://localhost:8080/test
$ curl -X POST --data "page=10&size=50" http://localhost:8080/test
Return: Page request [number: 10, size 50, sort: null]
返回:页面请求[数量:10,大小50,排序:空]
But, if you pass an json nothing happens:
但是,如果您传递一个 json,则什么也不会发生:
$ curl -X POST --data "{page:10&size:50}" http://localhost:8080/test
$ curl -X POST --data "{page:10&size:50}" http://localhost:8080/test
Return: Page request [number: 0, size 20, sort: null]
返回:页面请求[数量:0,大小20,排序:空]
回答by cwash
It seems to work just fine for me if you continue to provide them as query parameters on the URL, and still post data in.
如果您继续将它们作为 URL 上的查询参数提供,并且仍然发布数据,这对我来说似乎很好用。
POST http://localhost:8080/xyz?page=2&size=50
Content-Type: application/json
{
"filterABC": "data"
}
Spring seems to translate the page, size, sort etc. into the Pageable provided to the method on the way in.
Spring 似乎将页面、大小、排序等内容转换为提供给方法的 Pageable。
回答by UdayKiran Pulipati
Spring Post method
Spring Post 方法
@RequestMapping(value = "/quickSearchAction", method = RequestMethod.POST)
public @ResponseBody SearchList quickSearchAction(@RequestParam(value="userId") Long userId,
Pageable pageable) throws Exception {
return searchService.quickSearchAction(userId, pageable);
}
Postman Example:
邮递员示例:
http://localhost:8080/api/actionSearch/quickSearchAction?
userId=4451&number=0&size=20&sort=titleListId,DESC
In above POST Pageable is used for Sorting and Pagination in Spring RESTful service. Use below syntax at URL.
在上面的 POST Pageable 用于 Spring RESTful 服务中的排序和分页。在 URL 中使用以下语法。
number 0
, size 20
, Sort by field titleListId
and direction DESC
number 0
, size 20
, 按字段titleListId
和方向排序DESC
All passing parameter internally recognizes by Pageableas Sorting / Pagination parameters as below
所有传递参数在内部被Pageable识别为排序/分页参数,如下所示
number - Page number
size - Page Size
sort - sort by(Order by)
direction - ASC / DESC
回答by DwB
create a class that has the Pageable and QueryDto objects as members. Then pass JSON in the post body of this new object.
创建一个具有 Pageable 和 QueryDto 对象作为成员的类。然后在这个新对象的帖子正文中传递 JSON。
for example,
例如,
public class PageableQueryDto
{
private Pageable pageable;
private QueryDto queryDto;
... getters and setters.
}
EditAs noted in the comment below, you may need to implement the Pageable interface. The result could be something like this:
编辑如下面的评论中所述,您可能需要实现 Pageable 接口。结果可能是这样的:
public class PageableQueryDto implements Pageable
{
private Pageable pageable;
private QueryDto queryDto;
... getters and setters.
... Implement the Pageable interface. proxy all calls to the
... contained Pageable object.
... for example
public void blam()
{
pageable.blam();
}
... or maybe
public void blam()
{
if (pageable != null)
{
pageable.blam();
}
else
{
... do something.
}
}
回答by Chandra Prakash Reddy
sample example
示例
@RequestMapping(path = "/employees",method = RequestMethod.POST,consumes = "application/json",produces = "application/json")
ResponseEntity<Object> getEmployeesByPage(@RequestBody PageDTO page){
//creating a pagable object with pagenumber and size of the page
Pageable pageable= PageRequest.of(page.getPage(),page.getSize());
return ResponseEntity.status(HttpStatus.ACCEPTED).body(employeeRegistryService.getEmployeesByPage(pageable));
}
In your case try to add pagination variables in QueryDTO create a Pageable object and pass it to service
在您的情况下,尝试在 QueryDTO 中添加分页变量创建一个 Pageable 对象并将其传递给服务
I think that will solve :)
我认为这会解决:)