Rub*_*oga 5 java spring spring-data spring-boot
我是java和spring的新手.
我想要实现的是/tickets具有分页和排序的api端点.我做了它,它的工作原理.但也是我想做的是,如果返回所有门票的普通列表size,并page没有在查询参数指定的,所以在FE我可以使用选择框该列表.
我试图做的是getTickets在服务门面上实现并返回所有门票的列表.但我找不到如何检查Pageable是否设置的方法,因为它始终返回默认值(size = 20,page = 0)
//控制器
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Page<TicketListItemResponseModel>> getTickets(Pageable pageable) {
logger.info("> getTickets");
Page<TicketListItemResponseModel> tickets = ticketServiceFacade.getTickets(pageable);
logger.info("< getTickets");
return new ResponseEntity<>(tickets, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
// TicketServiceFacade
public Page<TicketListItemResponseModel> getTickets(Pageable pageable) {
Page<Ticket> tickets = ticketService.findAll(pageable);
return tickets.map(new ConverterFromPagesToListItem());
}
public List<TicketListItemResponseModel> getTickets() {
List<Ticket> tickets = ticketService.findAll();
return tickets.stream()
.map(t -> modelMapper.map(t, TicketListItemResponseModel.class))
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
也许我完全错了?
如果像这样构建控制器方法,则可以通过检查请求参数来管理是否要实现分页:
@Override
public ResponseEntity<Page<TicketListItemResponseModel>> getTickets(
@RequestParam(value = "page", defaultValue = "0", required = false) int page,
@RequestParam(value = "count", defaultValue = "10", required = false) int size,
@RequestParam(value = "order", defaultValue = "ASC", required = false) Sort.Direction direction,
@RequestParam(value = "sort", defaultValue = "name", required = false) String sortProperty) {
// here you would check your request params and decide whether or not to do paging and then return what you need to return
}
Run Code Online (Sandbox Code Playgroud)
如果您需要构建一个PageRequest来传递到您的服务方法中,则可以像这样手动进行:
new PageRequest(page, size, new Sort(direction, sortProperty));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3029 次 |
| 最近记录: |