简体   繁体   中英

How to skip the Paging attributes in Get Request in Springboot

I am new to REST APIs and I am developing a Get request. Previously I was sending all the objects in simple get request. Now I have implemented Pagination as well, for time being the goal is to let the from-end apis work and they will implement pagination gradually else they have to do lot of changes.

The previous api is {{base_url}}/api/leads/new

With Pagination {{base_url}}/api/leads/new?pageNumber=1&pageSize=10

Below is the code snippet.

The goal is to allow user to skip pageNumber and pageSize attributes along with their keys.

So that their previous api continues to work.

@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);
}

You can use default pagination like below

@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);
  }
}

Just change request url and replace from pageNumber to page and pageSize to size.

@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);
    }
}

Your service does allow both, if not set do not use paging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM