我是REST API的新手,正在开发Get请求。以前,我是通过简单的get请求发送所有对象的。现在,我也已经实现了分页功能,因为目前的目标是让后端api正常工作,并且它们将逐步实现分页功能,否则它们需要做很多更改。
上一个api是{{base_url}}/api/leads/new
具有分页功能{{base_url}}/api/leads/new?pageNumber=1&pageSize=10
下面是代码段。
目标是允许用户跳过pageNumber和pageSize属性及其键。
以便他们以前的api继续起作用。
@RequestMapping(value = "/new", method = RequestMethod.GET, params = { "pageNumber", "pageSize" } )
public @ResponseBody ResponseEntity getNewLeadsDetails(@RequestParam(value = "pageNumber" , required = false, defaultValue = "0") int pageNumber ,@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,Authentication authentication,
HttpServletRequest httpServletRequest) {
return leadService.getNewLeads(pageNumber,pageSize,authentication);
}
答案 0 :(得分:2)
您可以使用如下所示的默认分页
@RequestMapping(value = "/new", method = RequestMethod.GET)
public @ResponseBody ResponseEntity getNewLeadsDetails(Pageable pageable, Authentication authentication,
HttpServletRequest httpServletRequest) {
if(null == pageable) {
return leadService.getNewLeads(authentication);
} else {
return leadService.getNewLeads(pageable.getPageNumber(),pageable.getSize(),authentication);
}
}
只需更改请求url,然后从pageNumber替换为page,将pageSize替换为size。
答案 1 :(得分:0)
@RequestMapping(value = "/new", method = RequestMethod.GET, params = { "pageNumber", "pageSize" } )
public @ResponseBody ResponseEntity getNewLeadsDetails(@RequestParam(value = "pageNumber" , required = false) Integer pageNumber ,@RequestParam(value = "pageSize", required = false) Integer pageSize, Authentication authentication,
HttpServletRequest httpServletRequest) {
if(null == pageSize || null == pageNumber) {
return leadService.getNewLeads(authentication);
} else {
return leadService.getNewLeads(pageNumber,pageSize,authentication);
}
}
您的服务确实允许两者,如果未设置则不要使用分页。