在 Spring MVC 端点中使用多个 Pageable 参数

Mu *_* Pi 4 spring spring-mvc spring-data spring-data-jpa

我有这样的情况

@Autowired
private CustomerRepository repo;

@RequestMapping("/")
public Page<Customer> getDocuments(@Qualifier("bar") Pageable pageable,
                                   @Qualifier("foo")Pageable pageable2)
{
    return repo.findAll(pageable,pageable2.getOffset(), pageable2.getPageSize());
}
Run Code Online (Sandbox Code Playgroud)

但效果不佳。我的问题是,如何区分参数值。

为了实现上述场景,我不得不将我的方法更改为:

@RequestMapping("/")
public Page<Customer> getDocuments(Pageable pageable,
                                   @RequestParam(value="from", defaultValue="0") int offSet,
                                   @RequestParam(value="length", defaultValue="3") int length)
{
    return repo.findAll(pageable, offSet, length);
}
Run Code Online (Sandbox Code Playgroud)

Mu *_* Pi 7

终于,我找到了我的答案。我所做的完全正确,但我在我的 URL 中传递了错误的参数。

简而言之,如果您要传递两个 Pageable,则必须有一个 @Qualifier,并且您需要相应地设置您的请求参数。

我之前在 URL 中传递http://localhost:8080/?page=0&size=2&page=0&size=3 的内容,正确的 URL 是http://localhost:8080/?bar_page=0&bar_size=2&foo_page=0&foo_size=3

PS:无论谁拒绝了我的问题,我仍然生他/他们的气。