在post请求中传递可分页(spring数据)

时间:2016-01-20 00:47:28

标签: java spring rest

我有一个具有以下API的rest API服务器。 我有一些其他API,我可以从GET请求中分页。在这里,我需要发送一个传递queryDto的帖子请求。所以,我不能将page=0?size=20等作为url参数传递。

我想知道如何将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);
}

5 个答案:

答案 0 :(得分:7)

我认为这是不可能的,至少框架没有提供。

Spring有一个HandlerMethodArgumentResolver接口,其实现名为PageableHandlerMethodArgumentResolver,它检索调用HttpServletRequest.getParameter之类的请求参数值。因此,您可以绑定传递参数的Pageable实例&#34; page&#34;和&#34;尺寸&#34;用于GET和POST。因此,以下代码有效:

@RequestMapping(value="/test",method = RequestMethod.POST)
@ResponseBody
public String bindPage(Pageable page){
    return page.toString();
}

$ curl -X POST --data&#34; page = 10&amp; size = 50&#34; http://localhost:8080/test

返回: 页面请求[编号:10,大小50,排序:null]

但是,如果你通过json没有任何事情发生:

$ curl -X POST --data&#34; {page:10&amp; size:50}&#34; http://localhost:8080/test

返回: 页面请求[编号:0,大小20,排序:null]

答案 1 :(得分:1)

创建一个将Pageable和QueryDto对象作为成员的类。然后在这个新对象的post体中传递JSON。

例如,

public class PageableQueryDto
{
    private Pageable pageable;
    private QueryDto queryDto;

    ... getters and setters.
}

修改 如下面的注释所述,您可能需要实现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.
        }
}

答案 2 :(得分:1)

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);
}

邮递员示例:

http://localhost:8080/api/actionSearch/quickSearchAction?
userId=4451&number=0&size=20&sort=titleListId,DESC

在上面的POST Pageable用于Spring RESTful服务中的排序和分页。在URL上使用以下语法。

数字0,大小20,按字段titleListId和方向DESC排序

所有传递的参数在内部被 Pageable 识别为以下排序/分页参数

number - Page number

size - Page Size

sort - sort by(Order by)

direction - ASC / DESC

答案 3 :(得分:0)

如果您继续在URL上提供它们作为查询参数,并且仍然在其中发布数据,对我来说似乎很好。

POST http://localhost:8080/xyz?page=2&size=50
Content-Type: application/json
{
  "filterABC": "data"
}

Spring似乎将页面,大小,排序等转换为中途提供给方法的Pageable。

答案 4 :(得分:0)

示例示例

@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));
}

在您的情况下,请尝试在QueryDTO中添加分页变量 创建一个Pageable对象并将其传递给服务

我认为这将解决:)