springboot 中的多个 requestparam pageable

You*_*hky 2 restful-url http-request-parameters spring-boot pageable

我知道这个问题可能会重复,但我尝试了很多但没有成功

我需要在 spring boot rest 控制器中创建多个 RequestParam 如下:

@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);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试通过以下网址调用此服务时:

http://localhost:9116/api/prd-products/test?search=id==1,id==2&pageable=0&size=2 
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误

"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",
Run Code Online (Sandbox Code Playgroud)

当我尝试发送一个请求参数时,它成功工作。

注意:id==1,id==2 是在搜索中解析字段的方式

Nik*_*nko 8

使用 Spring Boot,您只需注册PageableHandlerMethodArgumentResolver bean

@Bean
public PageableHandlerMethodArgumentResolver pageableResolver() {
    return new PageableHandlerMethodArgumentResolver();
}
Run Code Online (Sandbox Code Playgroud)

在控制器中

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

然后将您的查询参数调整为 ...&page=0&size=2