假设我的 @RestController
GET 方法中有这个参数:
@PageableDefault(size = 20, sort = "updated_at", direction = Direction.DESC)
当我在没有指定sort
的情况下获取时,一切都很好:sort == update_at
和direction == DESC
。
但是当我 GET ...?page=1&size=33&sort=asdasd
时,它会忽略默认的 direction
并将其设置为 ASC
。
没有得到任何关于这是一个错误的结果。是不是?
最佳答案
@PageableDefault() 有默认方向 ASC 值,您可以添加如下
@SortDefault.SortDefaults({
@SortDefault(sort = "name", direction = Sort.Direction.DESC)
})
所以请求 Controller 看起来像
public ResponseEntity<Page<Brand>> findAll(
@PageableDefault(sort = { "name", "displayOrder" }, value = 10)
@SortDefault.SortDefaults({
@SortDefault(sort = "name", direction = Sort.Direction.DESC) })
Pageable pageable) {
Page<Brand> brandPage = brandService.findAll(pageable);
}
关于java - Spring @PageableDefault 默认方向在传递排序后发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38970086/