我使用spring 4.2并休息,我想使用分页。
弹簧支架控制器使用分页的方法是什么?
@RequestMapping(value = "/members/{memberId}/payments", method = RequestMethod.GET)
public Page<PaymentDto> getPaymentByMemberId(@PathVariable("memberId") Long memberId, Pageable pageable) {
return paymentService.getPaymentByMemberId(memberId, pageable);
}
这是管理这个的好方法吗?
如果对于应用程序中的某些区域,我们不想使用分页,我们需要创建另一个网址吗?
如果我想要为会员付款,我会这样做:
/members/{memberId}/payments
对于分页,有一种方法可以说是做了类似的事情:
/members/{memberId}/payments?pageNumber=1&PageSize=10
答案 0 :(得分:4)
一种方法是:
@RequestMapping(value = "/members/{memberId}/payments", method = RequestMethod.GET)
public List<PaymentDto> getPaymentByMemberId(@PathVariable("memberId") Long memberId, @RequestParam(value = "pageNumber", required = false) final Integer pageNumber,@RequestParam(value = "pageSize", required = false) final Integer pageSize) {
PageRequest pageReq = new PageRequest((pageNumber == null ? 0 : pageNumber), (pageSize == null ? 0 : pageSize));
Page<PaymentDto> page = paymentService.getPaymentByMemberId(memberId, pageReq);
return page.getContent();
}
答案 1 :(得分:0)
您需要为控制器编写注释@RestController
@RestController
public class PaymentController {
...
@RequestMapping(value = "/members/{memberId}/payments", method = RequestMethod.GET)
public Page<PaymentDto> getPaymentByMemberId(@PathVariable("memberId") Long memberId, Pageable pageable) {
return paymentService.getPaymentByMemberId(memberId, pageable);
}
}
请求示例:/ members / 12345 / payments?page = 0&size = 50