java Spring Data Pageable:返回空内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36618932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:41:06  来源:igfitidea点击:

Spring Data Pageable: Returns empty content

javaspringspring-mvcspring-dataspring-data-jpa

提问by yeddez

I'm new with Spring Data and Spring MVC and I don't understand why am I getting empty content:

我是 Spring Data 和 Spring MVC 的新手,我不明白为什么我会得到空内容:

@RequestMapping(value="/pages", method=RequestMethod.GET)
@ResponseBody 
public Page<Client> contactsPages(@RequestParam int page, @RequestParam int size) {
    Pageable pageable = new PageRequest(page, size, new Sort("id"));
    Page<Client> pageResult = clientRepository.findAll(pageable);

    return pageResult;
} 

The result of my json when I test the url is:

当我测试 url 时,我的 json 的结果是:

{"content":[],"last":true,"totalElements":2,"totalPages":1,"size":5,"number":1,"sort":[{"direction":"ASC","property":"id","ignoreCase":false,"nullHandling":"NATIVE","ascending":true}],"first":false,"numberOfElements":0}

And if you have good example making pageable request using Spring Data and Spring MVC and AngularJS; It will be a big help for me.

如果你有使用 Spring Data、Spring MVC 和 AngularJS 制作可分页请求的好例子;这对我来说将是一个很大的帮助。

回答by 8bitjunkie

Ensure that your PageRequestobject is requesting 0for small sets, not 1.

确保您的PageRequest对象请求的0是小集合,而不是1.

The pagination begins from 0.

分页从 开始0

This is a common mistake for beginners and is a common redherring when using @Queryin conjunction with Spring pagination. If your @Queryworks without pagination and then returns nothing when using it, check the page number.

这是初学者的常见错误,也是@Query与 Spring 分页结合使用时的常见错误。如果您的@Query作品没有分页,然后在使用时没有返回任何内容,请检查页码。