Reputation: 331
I have a rest API server which has the following API.
I have some other APIs, where i get pageable from GET
requests. Here, I need to make a post request for passing the queryDto. So, I cannot pass the page=0?size=20
etc as url parameters.
I would like to know how to pass pageable as JSON object to a POST
request
@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);
}
Upvotes: 16
Views: 52279
Reputation: 2209
An updated answer for spring boot:
Pageable
supports the following url params:
E.g.
Sample GET URL:
localhost:8080/users?page=2&size=10&sort=createdAt,DESC
@GetMapping("/users")
public ResponseEntity<?> getUsers(Pageable pageable) {
try {
return ResponseEntity.status(HttpStatus.OK).body(userService.getUsers(pageable));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
Upvotes: 0
Reputation: 6667
Spring Post method
@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 Example:
http://localhost:8080/api/actionSearch/quickSearchAction?
userId=4451&number=0&size=20&sort=titleListId,DESC
In above POST Pageable is used for Sorting and Pagination in Spring RESTful service. Use below syntax at URL.
number 0
, size 20
, Sort by field titleListId
and direction DESC
All passing parameter internally recognizes by Pageable as Sorting / Pagination parameters as below
number - Page number
size - Page Size
sort - sort by(Order by)
direction - ASC / DESC
Updated: Angular Example: CustomerComponent.ts file
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`
CustomerService.ts
getDesignations(fieldName: string, sortOrder: string): Observable<any> {
return this.httpClient.get("http://localhost:9876/api/getDesignations", {
params: {
sort: fieldName,sortOrder
}
});
}
Upvotes: 12
Reputation: 119
sample example
@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));
}
In your case try to add pagination variables in QueryDTO create a Pageable object and pass it to service
I think that will solve :)
Upvotes: 1
Reputation: 4245
It seems to work just fine for me if you continue to provide them as query parameters on the URL, and still post data in.
POST http://localhost:8080/xyz?page=2&size=50
Content-Type: application/json
{
"filterABC": "data"
}
Spring seems to translate the page, size, sort etc. into the Pageable provided to the method on the way in.
Upvotes: 4
Reputation: 38300
create a class that has the Pageable and QueryDto objects as members. Then pass JSON in the post body of this new object.
for example,
public class PageableQueryDto
{
private Pageable pageable;
private QueryDto queryDto;
... getters and setters.
}
Edit As noted in the comment below, you may need to implement the Pageable interface. The result could be something like this:
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.
}
}
Upvotes: 1
Reputation: 883
I Think that is not possible, at least not already provided by the framework.
The Spring has a HandlerMethodArgumentResolver
interface with an implementation called PageableHandlerMethodArgumentResolver
that retrieves the request param value calling something like HttpServletRequest.getParameter. So, you can bind the Pageable instance passing the parameters "page" and "size" for GET and POST. So, the following code works:
@RequestMapping(value="/test",method = RequestMethod.POST)
@ResponseBody
public String bindPage(Pageable page){
return page.toString();
}
$ curl -X POST --data "page=10&size=50" http://localhost:8080/test
Return: Page request [number: 10, size 50, sort: null]
But, if you pass an json nothing happens:
$ curl -X POST --data "{page:10&size:50}" http://localhost:8080/test
Return: Page request [number: 0, size 20, sort: null]
Upvotes: 12