spring 中的分页在我使用 findAllBy 包含和忽略大小写时不起作用?
Pagination in spring does not work when i use findAllBy Containing and Ignore Case?
有人可以向我解释为什么我的代码不起作用吗?我正在尝试创建一个按名称搜索食谱的端点。
当我尝试 return 列表时,我的代码有效。它 return 是正确的搜索结果:
@ResponseStatus(HttpStatus.OK)
@GetMapping(path = "api/v1/recipe", params = {"page", "size", "name"})
public List<Recipe> fetchAllRecipes(
@RequestParam(name = "page", required = true) Integer page,
@RequestParam(name = "size", required = true) Integer size,
@RequestParam(name = "name", required = true) String name
) {
return recipeRepository.findAllByNameContainingIgnoreCaseAndStatus(name, RecipeStatus.UPDATED);
}
然而,当我尝试对其进行分页时,内容总是空的:
@ResponseStatus(HttpStatus.OK)
@GetMapping(path = "api/v1/recipe", params = {"page", "size", "name"})
public Page<Recipe> fetchAllRecipes(
@RequestParam(name = "page", required = true) Integer page,
@RequestParam(name = "size", required = true) Integer size,
@RequestParam(name = "name", required = true) String name
) {
Pageable pageRequest = PageRequest.of(page, size, Sort.Direction.ASC, "id");
return recipeRepository.findAllByNameContainingIgnoreCaseAndStatus(name, RecipeStatus.UPDATED, pageRequest);
}
回复:
{
"content": [],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"pageNumber": 1,
"pageSize": 10,
"offset": 10,
"paged": true,
"unpaged": false
},
"last": true,
"totalPages": 1,
"totalElements": 2,
"first": false,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"number": 1,
"numberOfElements": 0,
"size": 10,
"empty": true
}
以下是我正在使用的依赖项:
'org.springframework.boot:spring-boot-starter'
'org.springframework.boot:spring-boot-starter-web'
'org.springframework.boot:spring-boot-starter-data-jpa'
'org.springframework.boot:spring-boot-starter-test'
PageRequest
的页面索引是 zero-based,这就是为什么所有 2 个现有元素都在页面 0
和页面上1
为空。
有人可以向我解释为什么我的代码不起作用吗?我正在尝试创建一个按名称搜索食谱的端点。
当我尝试 return 列表时,我的代码有效。它 return 是正确的搜索结果:
@ResponseStatus(HttpStatus.OK)
@GetMapping(path = "api/v1/recipe", params = {"page", "size", "name"})
public List<Recipe> fetchAllRecipes(
@RequestParam(name = "page", required = true) Integer page,
@RequestParam(name = "size", required = true) Integer size,
@RequestParam(name = "name", required = true) String name
) {
return recipeRepository.findAllByNameContainingIgnoreCaseAndStatus(name, RecipeStatus.UPDATED);
}
然而,当我尝试对其进行分页时,内容总是空的:
@ResponseStatus(HttpStatus.OK)
@GetMapping(path = "api/v1/recipe", params = {"page", "size", "name"})
public Page<Recipe> fetchAllRecipes(
@RequestParam(name = "page", required = true) Integer page,
@RequestParam(name = "size", required = true) Integer size,
@RequestParam(name = "name", required = true) String name
) {
Pageable pageRequest = PageRequest.of(page, size, Sort.Direction.ASC, "id");
return recipeRepository.findAllByNameContainingIgnoreCaseAndStatus(name, RecipeStatus.UPDATED, pageRequest);
}
回复:
{
"content": [],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"pageNumber": 1,
"pageSize": 10,
"offset": 10,
"paged": true,
"unpaged": false
},
"last": true,
"totalPages": 1,
"totalElements": 2,
"first": false,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"number": 1,
"numberOfElements": 0,
"size": 10,
"empty": true
}
以下是我正在使用的依赖项:
'org.springframework.boot:spring-boot-starter'
'org.springframework.boot:spring-boot-starter-web'
'org.springframework.boot:spring-boot-starter-data-jpa'
'org.springframework.boot:spring-boot-starter-test'
PageRequest
的页面索引是 zero-based,这就是为什么所有 2 个现有元素都在页面 0
和页面上1
为空。