我有一个其他API服务器,它具有以下API.我有一些其他的API,我可以从GET请求中分页.在这里,我需要发送一个传递queryDto的帖子请求.所以,我不能将page=0?size=20etc作为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);
}
Run Code Online (Sandbox Code Playgroud)
我认为这是不可能的,至少框架没有提供.
Spring有一个HandlerMethodArgumentResolver带有实现的接口,该实现调用PageableHandlerMethodArgumentResolver检索请求param值,调用类似HttpServletRequest.getParameter的东西.因此,您可以绑定传递参数"page"和"size"的Pageable实例以进行GET和POST.因此,以下代码有效:
@RequestMapping(value="/test",method = RequestMethod.POST)
@ResponseBody
public String bindPage(Pageable page){
return page.toString();
}
Run Code Online (Sandbox Code Playgroud)
$ curl -X POST --data"page = 10&size = 50" http:// localhost:8080/test
返回:页面请求[编号:10,大小50,排序:null]
但是,如果你传递一个json没有任何反应:
$ curl -X POST --data"{page:10&size:50}" http:// localhost:8080/test
返回:页面请求[编号:0,大小20,排序:null]
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);
}
Run Code Online (Sandbox Code Playgroud)
邮递员示例:
http://localhost:8080/api/actionSearch/quickSearchAction?
userId=4451&number=0&size=20&sort=titleListId,DESC
Run Code Online (Sandbox Code Playgroud)
在上面 POST Pageable 用于 Spring RESTful 服务中的排序和分页。在 URL 中使用以下语法。
number 0, size 20, 按字段titleListId和方向排序DESC
所有传递参数在内部被Pageable识别为排序/分页参数,如下所示
number - Page number
size - Page Size
sort - sort by(Order by)
direction - ASC / DESC
Run Code Online (Sandbox Code Playgroud)
更新:Angular 示例:CustomerComponent.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 { return this.httpClient.get("http://localhost:9876/api/getDesignations", { params: { sort: fieldName,sortOrder } }); }