Yousef Al Kahky
Yousef Al Kahky

Reputation: 713

multiple requestparam in springboot pageable

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

Upvotes: 3

Views: 8731

Answers (3)

Nikolai  Shevchenko
Nikolai Shevchenko

Reputation: 7521

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

Upvotes: 11

Matheus
Matheus

Reputation: 3370

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.

Upvotes: 0

Gustavo Passini
Gustavo Passini

Reputation: 2668

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

Upvotes: 2

Related Questions