如何Pageable
仅使用排序信息将对象传递给 Spring JPA Repository?我想要这样的东西:
Pageable pageable = PageRequest.of(null, null, Sort());
Run Code Online (Sandbox Code Playgroud)
我知道它需要int
aspage
和size
但是如果我们不能以某种方式使用这些值并给出一个未分页的结果
Sort.by(Sort.Direction.ASC, "age")
我们可以对页面和大小使用像1000
,这样的大数字,1000
但我不想对这些值进行硬编码。
我也不打算为Pageable
.
请让我知道如何实现这一目标。
提前致谢。
我使用 Mockito 为控制器中的方法编写了一个非常简单的测试
@Test
fun `get items based on category ID`() {
val pageable: Pageable = PageRequest.of(5, 50)
controller.get(10, pageable)
val captor = ArgumentCaptor.forClass(Int::class.java)
val pageableCaptor = ArgumentCaptor.forClass(Pageable::class.java)
Mockito.verify(itemService).getItemsBasedOnCategoryID(captor.capture(), pageableCaptor.capture())
assertEquals(captor.value, 10)
assertEquals(pageableCaptor.value.pageSize, 50)
assertEquals(pageableCaptor.value.pageNumber, 5)
}
Run Code Online (Sandbox Code Playgroud)
但我得到这个例外
pageableCaptor.capture() must not be null
java.lang.NullPointerException: pageableCaptor.capture() must not be null
at com.practice.ItemControllerTest.get items based on category ID(ItemControllerTest.kt:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Run Code Online (Sandbox Code Playgroud)
我无法理解,因为当我使用类似的代码直接在服务层上测试该方法时,它通过了测试。我有一个针对此测试的解决方法,但我只是想了解为什么这不起作用。我真的很感激一些帮助。
如果您希望我添加任何其他信息,请随时告诉我。
我知道这个问题可能会重复,但我尝试了很多但没有成功
我需要在 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);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试通过以下网址调用此服务时:
http://localhost:9116/api/prd-products/test?search=id==1,id==2&pageable=0&size=2
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误
"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 …
Run Code Online (Sandbox Code Playgroud) 我想根据表元素的条目索引提供实际页面。我用PageRequest.of(int page, int size)
.
我有index
传递给函数的 ,以及一个PAGESIZE_SEARCH
50的常量。
我想根据索引传递当前页面如下:
PageRequest.of((int) index/PAGESIZE_SEARCH, PAGESIZE_SEARCH)
Run Code Online (Sandbox Code Playgroud)
我的 IDEA(IntelliJ Ultimate 2020.1.1)使我的投射和暗示变灰 Casting 'index' to 'int' is redundant
。如果我删除演员表,即使编译器接受它,我也不会收到运行时异常。
为什么是这样?那里的部门不是不安全吗?任何解释将不胜感激。
pageable ×4
java ×2
hibernate ×1
jpa ×1
kotlin ×1
mockito ×1
restful-url ×1
spring-boot ×1
unit-testing ×1