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

jq6vz3qz  于 2023-01-25  发布在  Spring
关注(0)|答案(5)|浏览(578)

我有一个休息API服务器,它有以下API。我有一些其他的API,在那里我从GET请求中获得可分页。这里,我需要发出一个post请求来传递queryDto。所以,我不能将page=0?size=20等作为url参数传递。
我想知道如何将可分页作为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);
}
myzjeezk

myzjeezk1#

我认为这是不可能的,至少框架还没有提供。
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开机自检--数据"页面= 10且大小= 50" http://localhost:8080/test
返回:页面请求[编号:10,尺寸50,排序:无效]
但是,如果您传递一个json,则什么也不会发生:
$curl-X开机自检--数据"{第10页和大小:50}" http://localhost:8080/test
返回:页面请求[编号:0,大小20,排序:无效]

mi7gmzs6

mi7gmzs62#

Spring柱法

@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 示例:

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

更新:Angular 示例:客户组件.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

getDesignations(fieldName: string, sortOrder: string): Observable<any> {
    return this.httpClient.get("http://localhost:9876/api/getDesignations", {
      params: {
        sort: fieldName,sortOrder
      }
    });
  }
0dxa2lsx

0dxa2lsx3#

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

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

Spring似乎将页面、大小、排序等转换为在进入过程中提供给方法的Pageable。

twh00eeo

twh00eeo4#

创建一个包含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.
        }
}
ttisahbt

ttisahbt5#

样本

@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对象并将其传递给service
我想那会解决:)

相关问题