Spring 页面的totalElements 和totalPages 值不正确

发布于 2025-01-11 15:40:18 字数 1884 浏览 0 评论 0原文

我的数据库中有 21 行,我正在使用 Spring 的 Page 来获取它们。

如果我指定的页面大小小于22,它将显示totalElements: 3864,并且totalPages也是错误的。可能出什么问题了?

例子: GET myurl.com/foo?size=5

产生如下结果

{
   
        "content": [
            {
               { "pretend_that": "there are five elements here" }
            }
        ],
        "pageable": {
            "sort": {
                "empty": true,
                "sorted": false,
                "unsorted": true
            },
            "offset": 0,
            "pageNumber": 0,
            "pageSize": 5,
            "paged": true,
            "unpaged": false
        },
        "last": false,
        "totalPages": 773,
        "totalElements": 3864,
        "size": 5,
        "number": 0,
        "sort": {
            "empty": true,
            "sorted": false,
            "unsorted": true
        },
        "first": true,
        "numberOfElements": 5,
        "empty": false
}

示例 2:

GET myurl.com/foo?size=50

结果

{
    
        "content": [
            {
                { "pretend_that": "there are 21 elements here" }
            }
        ],
        "pageable": {
            "sort": {
                "empty": true,
                "sorted": false,
                "unsorted": true
            },
            "offset": 0,
            "pageNumber": 0,
            "pageSize": 50,
            "paged": true,
            "unpaged": false
        },
        "last": true,
        "totalPages": 1,
        "totalElements": 21,
        "size": 50,
        "number": 0,
        "sort": {
            "empty": true,
            "sorted": false,
            "unsorted": true
        },
        "first": true,
        "numberOfElements": 21,
        "empty": false
    
}

产生如下 如果我得到最后一页,它会正确显示页面和元素计数。

我尝试过更改 Spring Boot 版本,但没有帮助。

I have 21 rows in a database and I am using Spring's Page to get them.

If I specify a page size less than 22, it will show totalElements: 3864 and totalPages is also wrong. What could be wrong?

Example:
GET myurl.com/foo?size=5

yields a result like this

{
   
        "content": [
            {
               { "pretend_that": "there are five elements here" }
            }
        ],
        "pageable": {
            "sort": {
                "empty": true,
                "sorted": false,
                "unsorted": true
            },
            "offset": 0,
            "pageNumber": 0,
            "pageSize": 5,
            "paged": true,
            "unpaged": false
        },
        "last": false,
        "totalPages": 773,
        "totalElements": 3864,
        "size": 5,
        "number": 0,
        "sort": {
            "empty": true,
            "sorted": false,
            "unsorted": true
        },
        "first": true,
        "numberOfElements": 5,
        "empty": false
}

Example 2:

GET myurl.com/foo?size=50

yields a result like this

{
    
        "content": [
            {
                { "pretend_that": "there are 21 elements here" }
            }
        ],
        "pageable": {
            "sort": {
                "empty": true,
                "sorted": false,
                "unsorted": true
            },
            "offset": 0,
            "pageNumber": 0,
            "pageSize": 50,
            "paged": true,
            "unpaged": false
        },
        "last": true,
        "totalPages": 1,
        "totalElements": 21,
        "size": 50,
        "number": 0,
        "sort": {
            "empty": true,
            "sorted": false,
            "unsorted": true
        },
        "first": true,
        "numberOfElements": 21,
        "empty": false
    
}

P.S. if I get the final page, it shows the page and elements count correctly.

I have tried changing Spring Boot versions and that hasn't helped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文