Java 在post请求中传递可分页(spring数据)
我有一个RESTAPI服务器,它具有以下API。 我还有一些其他的API,可以从Java 在post请求中传递可分页(spring数据),java,spring,rest,Java,Spring,Rest,我有一个RESTAPI服务器,它具有以下API。 我还有一些其他的API,可以从get请求中获得页面。在这里,我需要发出一个post请求来传递queryTo。因此,我无法将page=0?size=20等作为url参数传递 我想知道如何将pageable作为JSON对象传递给POST请求 @RequestMapping(value = "/internal/search", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) @R
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);
}
@RequestMapping(value=“/internal/search”,method=RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@应答器
公共响应列表findObjects(@RequestBody QueryDto QueryDto,Pageable Pageable){
if(queryDto.isEmpty()){
抛出新的BadRequestException();
}
return someService.findObjectsByQuery(queryDto,可分页);
}
我认为这是不可能的,至少框架还没有提供
Spring具有一个名为PageableHandlerMethodArgumentResolver
的实现的HandlerMethodArgumentResolver
接口,该实现通过调用类似HttpServletRequest.getParameter的内容检索请求参数值。因此,您可以通过GET和POST的参数“page”和“size”绑定可分页实例。因此,以下代码起作用:
@RequestMapping(value=“/test”,method=RequestMethod.POST)
@应答器
公共字符串绑定页(可分页页){
返回page.toString();
}
$curl-X POST--data“page=10,size=50”
返回:
页面请求[编号:10,大小50,排序:空]
但是,如果传递一个json,则不会发生任何事情:
$curl-X POST--数据“{page:10&size:50}”
返回:
页面请求[number:0,size:20,sort:null]创建一个类,该类的成员包括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.
}
}
如果您继续在URL上提供它们作为查询参数,并且仍然在中发布数据,那么对我来说似乎工作得很好
POST http://localhost:8080/xyz?page=2&size=50
Content-Type: application/json
{
"filterABC": "data"
}
Spring似乎将页面、大小、排序等转换为在方法进入过程中提供给该方法的可分页内容。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
在上面的示例中,PostPageable用于SpringRESTful服务中的排序和分页。在URL处使用以下语法
编号0
,大小20
,按字段排序标题列表
和方向DESC
所有通过的参数在内部通过可分页识别为排序/分页参数,如下所示
number - Page number
size - Page Size
sort - sort by(Order by)
direction - ASC / DESC
更新:
角度示例:
CustomerComponent.ts文件
让resultDesignations=null;让fieldName=“指定ID”;this.customerService.getDesignations(fieldName,“DESC”).subscribe((数据)=>{resultDesignations=data;},(err)=>{this.error(err.error);},()=>{this.designations=resultDesignations;})//结束日期名称
CustomerService.ts
GetSignatures(fieldName:string,sortOrder:string):可观察{
返回此.httpClient.get(“http://localhost:9876/api/getDesignations", {
参数:{
排序:字段名,排序器
}
});
}示例
@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));
}
@RequestMapping(path=“/employees”,method=RequestMethod.POST,consumes=“application/json”,products=“application/json”)
ResponseEntity getEmployeesByPage(@RequestBody PageDTO page){
//创建具有页码和页面大小的可分页对象
Pageable Pageable=PageRequest.of(page.getPage(),page.getSize());
返回ResponseEntity.status(HttpStatus.ACCEPTED).body(employeeRegistryService.getEmployeesByPage(可分页));
}
在您的情况下,尝试在QueryDTO中添加分页变量
创建可分页对象并将其传递给服务
我认为这会解决:)似乎是一个很好的解决方案。但是,有一个疑问:他可能需要为“Pageable”对象公开一些其他接口,因为Pageable是一个接口,框架实现没有默认的构造函数或页面和大小设置器。你不这么认为吗?如果你把页面和大小作为查询参数,对我来说效果很好。看看我的答案。我们如何从ajax javascript代码发送这些参数。这个答案非常有效。在我的例子中,我使用RequestBody和pageable执行GET请求。提供页面和大小作为查询参数允许请求成功通过。