3

i know that this question may be duplicated but i tried a lot with no success

I need to make multiple RequestParam in spring boot rest controller as following :

@GetMapping("/prd-products/test")
@Timed
public ResponseEntity<List<Test>> getAllTests(@RequestParam (required = false) String search, @RequestParam (required = false) Pageable pageable) {
    Page<Test> page = prdProductsService.findAllTest(search, pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/prd-products");
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}

when i try to call this service by url like :

http://localhost:9116/api/prd-products/test?search=id==1,id==2&pageable=0&size=2 

it give me following error

"title": "Bad Request",
"status": 400,
"detail": "Failed to convert value of type 'java.lang.String' to required type 'org.springframework.data.domain.Pageable'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.data.domain.Pageable': no matching editors or conversion strategy found",

when i try to send one request param it successfully work .

Note : id==1,id==2 is the way that fields is parsed in search

Yousef Al Kahky
  • 703
  • 1
  • 8
  • 23

3 Answers3

10

With Spring Boot you can just register PageableHandlerMethodArgumentResolver bean

@Bean
public PageableHandlerMethodArgumentResolver pageableResolver() {
    return new PageableHandlerMethodArgumentResolver();
}

In controller

public ResponseEntity<List<Test>> getAllTests(
    @RequestParam (required = false) String search,
    @PageableDefault(page = 0, size = 100) Pageable pageable
) {
    ...
}

And then adjust your query parameters to ...&page=0&size=2

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
1

Spring cannot convert a String to a Pageable. You should create a Pageable object from the request parameters, for example, with PageRequest.of.

Example:

@GetMapping("/prd-products/test")
@Timed
public ResponseEntity<List<Test>> getAllTests(
        @RequestParam (required = false) String search,
        @RequestParam (required = false) Integer pageIndex,
        @RequestParam (required = false) Integer pageSize) {
    Pageable pageable = PageRequest.of(pageIndex, pageSize);
    Page<Test> page = prdProductsService.findAllTest(search, pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/prd-products");
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
Gustavo Passini
  • 2,348
  • 19
  • 25
0

Your QueryString seems strange.

In order to use Spring Data REST to create and inject a Pageable, you need to do something like this:

?page=0&size=2&ids=1,2

Your REST Controller:

public ResponseEntity <List<Test>> getAllTests(@RequestParam(required = false) String search, @RequestParam(required = false) Pageable pageable) {
    Page<Test> page = prdProductsService.findAllTest(search, pageable);
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}

This way, Spring Data REST will create a Pageable object.

See reference documentation.

Matheus
  • 3,058
  • 7
  • 16
  • 37