有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个RESTAPI服务器,它具有以下API。 我还有一些其他API,可以从GET请求中获得分页。在这里,我需要发出一个post请求来传递queryTo。因此,我无法将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) 个答案

  1. # 1 楼答案

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

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

    Spring似乎将页面、大小、排序等转换为在方法进入时提供给该方法的可分页内容

  2. # 2 楼答案

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

    比如说,

    public class PageableQueryDto
    {
        private Pageable pageable;
        private QueryDto queryDto;
    
        ... getters and setters.
    }
    

    编辑 如下面的注释所述,您可能需要实现可分页接口。 结果可能是这样的:

    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.
            }
    }
    
  3. # 3 楼答案

    我认为这是不可能的,至少框架尚未提供

    Spring有一个HandlerMethodArgumentResolver接口,该接口具有一个名为PageableHandlerMethodArgumentResolver的实现,该实现检索调用类似HttpServletRequest的请求参数值。获取参数。因此,您可以通过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

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

    但是,如果传递一个json,则不会发生任何事情:

    $curl-X POST--数据“{page:10&;size:50}”http://localhost:8080/test

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

  4. # 4 楼答案

    示例

    @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中添加分页变量 创建可分页对象并将其传递给服务

    我认为这将解决:)

  5. # 5 楼答案

    弹簧柱法

    @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
    

    在上面的示例中,PostPageable用于SpringRESTful服务中的排序和分页。在URL处使用以下语法

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

    所有通过的参数在内部通过可分页识别为排序/分页参数,如下所示

    number - Page number
    
    size - Page Size
    
    sort - sort by(Order by)
    
    direction - ASC / DESC
    

    更新: 角度示例: 客户组件。ts文件

    let resultDesignations = null; let fieldName = "designationId"; this.customerService.getDesignations(fieldName, "DESC").subscribe( (data) => { resultDesignations = data; }, (err) => { this.error(err.error); }, () => { this.designations = resultDesignations; } );//END getDesignations

    客户服务。ts

    GetSignatures(fieldName:string,sortOrder:string):可观察{ 把这个还给我。httpClient。得到(”http://localhost:9876/api/getDesignations", { 参数:{ 排序:字段名,排序器 } }); }