使用GET方法在Spring Boot控制器中传递Pageable参数

huangapple go评论58阅读模式
英文:

Passing Pageable parameter with GET method in Spring Boot Controller

问题

我只是尝试将页面和大小参数传递给以下的Controller方法,但据我所见,似乎无法在不使用POST方法的情况下实现。

@GetMapping
ResponseEntity<Page<PostDto>> getPostList(@RequestBody Pageable pageable) {
    final Page<PostDto> response = postService.getPostList(pageable);
    
    // 代码已省略
    }

这是我的Postman请求和主体:

{
    "pageNumber": 0,
    "pageSize": 3
}

那么,如何正确通过GET方法传递这两个参数呢?

这是错误消息:

>"类型定义错误:[simple type, class org.springframework.data.domain.Pageable];嵌套异常是com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造org.springframework.data.domain.Pageable的实例(没有创建者,如默认构造函数存在):抽象类型需要映射到具体类型,具有自定义反序列化程序,或包含附加类型信息\n at [Source: (PushbackInputStream); line: 1, column: 1]"


<details>
<summary>英文:</summary>

I am just trying to pass page and size parameters to the following Controller method, but as far as I see, it does not seem to be possible without POST method. 

@GetMapping
ResponseEntity<Page<PostDto>> getPostList(@RequestBody Pageable pageable) {
final Page<PostDto> response = postService.getPostList(pageable);

// code omitted
}

Here is my Postman request and body:

{
"pageNumber": 0,
"pageSize": 3
}


So, how can I pass these 2 parameters via GET method properly? 

Here is the error message:

&gt;&quot;Type definition error: [simple type, class org.springframework.data.domain.Pageable]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.data.domain.Pageable` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information\n at [Source: (PushbackInputStream); line: 1, column: 1]&quot;
}

</details>


# 答案1
**得分**: 1

你可以传递给方法2个查询参数:pageNumber和pageSize。

```java
@GetMapping("/getPostList")
public ResponseEntity<Page<PostDto>> getPostList(
               @RequestParam Integer pageNumber,
               @RequestParam Integer pageSize) {
    var pageable = Pageable.ofSize(pageSize).withPage(pageNumber);
    
    final Page<PostDto> response = postService.getPostList(pageable);

    // 代码部分省略
}

在这个方法中,只需基于这两个参数创建一个Pageable对象。

你的请求URL将如下所示:

http://localhost:8080/getPostList?pageNumber=1&pageSize=20

通常将这些参数放在查询中是一种常见的做法。

希望这对你有所帮助 使用GET方法在Spring Boot控制器中传递Pageable参数

英文:

You can pass to the method 2 query parameters: pageNumber and pageSize

@GetMapping(&quot;/getPostList&quot;)
public ResponseEntity&lt;Page&lt;PostDto&gt;&gt; getPostList(
               @RequestParam Integer pageNumber,
               @RequestParam Integer pageSize) {
    var pageable = Pageable.ofSize(pageSize).withPage(pageNumber);
    
    final Page&lt;PostDto&gt; response = postService.getPostList(pageable);

    // code omitted
}

In the method just create a Pageable object based on this two params.

Your request URL will be look like:

http://localhost:8080/getPostList?pageNumber=1&amp;pageSize=20

It is pretty common practice to put these parameters in query.

Hope it will helps you 使用GET方法在Spring Boot控制器中传递Pageable参数

答案2

得分: 1

控制器方法可以定义为:

@GetMapping
ResponseEntity<Page<PostDto>> getPostList(Pageable pageable) {
    final Page<PostDto> response = postService.getPostList(pageable);
    // code omitted
}

上述方法签名会让 Spring MVC 尝试从请求参数中推导出一个 Pageable 实例,使用以下默认配置:

  • page - 要检索的页面。从0开始索引,默认为0。
  • size - 要检索的页面大小。默认为20。
  • sort - 应该按照的属性格式进行排序,例如 property,property(,ASC|DESC)(,IgnoreCase)。默认排序方向是区分大小写的升序。如果要更改方向或大小写敏感性,可以使用多个 sort 参数,例如 ?sort=firstname&sort=lastname,asc&sort=city,ignorecase

供参考:Spring文档 - 用于Pageable和Sort的HandlerMethodArgumentResolvers

英文:

The controller method can be defined as:

@GetMapping
ResponseEntity&lt;Page&lt;PostDto&gt;&gt; getPostList(Pageable pageable) {
    final Page&lt;PostDto&gt; response = postService.getPostList(pageable);
    // code omitted
}

> The preceding method signature causes Spring MVC to try to derive a
> Pageable instance from the request parameters by using the following
> default configuration:
>
> - page - Page you want to retrieve. 0-indexed and defaults to 0.
>
> - size - Size of the page you want to retrieve. Defaults to 20.
>
> - sort - Properties that should be sorted by in the format property,property(,ASC|DESC)(,IgnoreCase). The default sort
> direction is case-sensitive ascending. Use multiple sort parameters if you want to switch direction or case sensitivity — for example, ?sort=firstname&amp;sort=lastname,asc&amp;sort=city,ignorecase.

For reference: Spring Docs - HandlerMethodArgumentResolvers for Pageable and Sort

答案3

得分: 0

最佳做法是通过查询参数而不是请求体传递GET请求中的参数。因此,请考虑将其更改为:

ResponseEntity&lt;Page&lt;PostDto&gt;&gt; getPostList(
   @RequestParam(name = "pageNumber", defaultValue="0") Integer pageNumber,
   @RequestParam(name = "pageSize" , defaultValue="20") Integer pageSize) {

  Pageable pageable = Pageable.ofSize(pageSize).withPage(pageNumber);
   // 省略的代码
 }

然后通过以下方式调用端点:

GET /posts?pageNumber=1&pageSize=10
英文:

The best practise for passing the parameter in GET request is through query parameters but not the request body. So consider to change it to :

ResponseEntity&lt;Page&lt;PostDto&gt;&gt; getPostList(
   @RequestParam(name = &quot;pageNumber&quot;,defaultValue=&quot;0&quot;) Integer pageNumber,
   @RequestParam(name = &quot;pageSize&quot;  ,defaultValue=&quot;20&quot;) Integer pageSize) {

  Pageable pageable = Pageable.ofSize(pageSize).withPage(pageNumber);
   // code omitted
 }

Then call the endpoint by :

GET /posts?pageNumber=1&amp;pageSize=10

huangapple
  • 本文由 发表于 2023年2月27日 00:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定