【问题标题】:multiple requestparam in springboot pageablespringboot中的多个requestparam可分页
【发布时间】:2019-08-21 11:31:32
【问题描述】:

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

我需要在 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);
}

当我尝试通过 url 调用此服务时:

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

它给了我以下错误

"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 Data Rest 吗?您的 url 看起来不像 Spring Data Rest 默认工作的模式..
  • 是的,我使用弹簧架,你能提供给我你的意思的默认模式吗?

标签: spring-boot restful-url http-request-parameters pageable


【解决方案1】:

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

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

在控制器中

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

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

【讨论】:

    【解决方案2】:

    Spring 无法将 String 转换为 Pageable。您应该根据请求参数创建一个 Pageable 对象,例如使用PageRequest.of

    例子:

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

    【讨论】:

    • 它工作正常,但以我的方式发送之前,为什么当我单独发送它而没有其他请求参数时它接受可分页?
    • 你发送的唯一参数是pageable,或者search
    • 对不起,我不知道它是如何从一个名为 pageable 的请求参数创建一个 Pageable 对象的。
    【解决方案3】:

    您的 QueryString 看起来很奇怪。

    为了使用 Spring Data REST 创建和注入 Pageable,您需要执行以下操作:

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

    您的 REST 控制器:

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

    这样,Spring Data REST 将创建一个 Pageable 对象。

    reference documentation

    【讨论】:

    • 我无法制作 id 列表,因为搜索字符串是一种动态搜索方式,它接受实体的任何属性和任何操作,不仅 ==,它必须是动态方式
    • 不,它给了我同样的错误,这是我以前做过的
    • 您确定您使用的是 Sprint DATA REST? org.springframework.bootspring-boot-starter-data-rest
    猜你喜欢
    • 2019-01-22
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2019-07-04
    相关资源
    最近更新 更多