Spring boot springboot中的多个requestparam可分页

Spring boot springboot中的多个requestparam可分页,spring-boot,restful-url,http-request-parameters,pageable,Spring Boot,Restful Url,Http Request Parameters,Pageable,我知道这个问题可能会重复,但我尝试了很多,但没有成功 我需要在spring boot rest控制器中创建多个RequestParam,如下所示: @GetMapping("/prd-products/test") @Timed public ResponseEntity<List<Test>> getAllTests(@RequestParam (required = false) String search, @RequestParam (required = fal

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

我需要在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);
}
它给我以下的错误

"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",
当我尝试发送一个请求参数时,它会成功工作


注意:id==1,id==2是在搜索中解析字段的方式,Spring无法将字符串转换为可分页的字符串。您应该根据请求参数创建一个可分页对象,例如,使用

例如:

@GetMapping/prd产品/测试 @定时 公众反应能力测试 @RequestParam required=假字符串搜索, @RequestParam required=假整数pageIndex, @RequestParam required=假整数pageSize{ Pageable Pageable=PageRequest.ofpageIndex,pageSize; Page Page=prdProductsService.findAllTestsearch,可分页; HttpHeaders=PaginationUtil.generatePaginationHttpHeaderspage,/api/prd products; 返回新的ResponseEntitypage.getContent、Header、HttpStatus.OK; }
你的质询似乎很奇怪

为了使用Spring Data REST创建和注入可分页文件,您需要执行以下操作:

?页面=0,尺寸=2,ID=1,2

您的REST控制器:

公共责任getAllTests@RequestParamrequired=false字符串搜索,@RequestParamrequired=false可分页可分页{ Page Page=prdProductsService.findAllTestsearch,可分页; 返回新的ResponseEntitypage.getContent、Header、HttpStatus.OK; } 这样,SpringDataREST将创建一个可分页对象


请参阅。

使用Spring Boot,您只需注册bean即可

内部控制器

public ResponseEntity<List<Test>> getAllTests(
    @RequestParam (required = false) String search,
    @PageableDefault(page = 0, size = 100) Pageable pageable
) {
    ...
}
然后将查询参数调整为
…&page=0&size=2

您使用的是Spring数据Rest吗?你的url看起来不像Spring Data Rest默认工作的模式。是的,我使用Spring Rest,你能提供你的默认模式吗?我不能列出ID,因为搜索字符串是一种动态搜索方式,它接受实体的任何属性和任何操作,不仅==,它必须是动态的不,它给了我同样的错误,这是我以前做的你确定你在使用Sprint数据REST吗?org.springframework.bootspring-boot-starter-data-resit工作正常,但以我之前发送的方式,当我单独发送时,没有其他请求参数,为什么它接受pageable?您发送的唯一参数是pageable,或者搜索?对不起,我不知道它如何能够从一个名为Pageable的请求参数创建一个可分页对象。
@Bean
public PageableHandlerMethodArgumentResolver pageableResolver() {
    return new PageableHandlerMethodArgumentResolver();
}
public ResponseEntity<List<Test>> getAllTests(
    @RequestParam (required = false) String search,
    @PageableDefault(page = 0, size = 100) Pageable pageable
) {
    ...
}