0
I created a method within a service class to generate a paged list of products using Springdata (Java Spring Rest application), follow the code:
public Page<ProdutoDTOVendedor> listarProdutos(String descricao, Boolean disponivel,
Integer page, Integer size, String orderBy, String direction) {
//Rotina para validação do usuário do end-point
UserSS us = AuthService.authenticated();
Optional<Vendedor> vendedor = vendedorRepository.findById(us.getId());
if (!vendedor.isPresent()) {
throw new ObjectNotFoundException("Vendedor não encontrado");
}
Pageable pageable = PageRequest.of(page, size, Direction.valueOf(direction), orderBy);
List<ProdutoDTOVendedor> produtosVendedor = new ArrayList<>();
Optional<List<ItemVendedor>> itensVendedor = itemVendedorRepository.findByIdVendedorEqualsAndIdProdutoDescricaoDetalhadaContaining(vendedor.get(), descricao);
for (ItemVendedor item : itensVendedor.get()) {
produtosVendedor.add(itemVendedorToProdutoDTOVendedor(item));
}
Page<ProdutoDTOVendedor> produtosPage = new PageImpl<ProdutoDTOVendedor>(produtosVendedor,pageable, (pageable.getOffset() + pageable.getPageSize()));
return produtosPage;
}
The method generates the following paged list:
If you understand the attribute says this with the value 24, so it can bring the 4 elements of the list because it is the total that I have registered, but if I change this attribute size, the list of items should come with the equivalent amount if the assigned value is less than I have of registered products:
As you can see I put in the URI the size parameter, the pageable object received and recognized the size parameter correctly, but it did not have an effect in the product list, with the parameter in the value 1 should return only 1 item and not the total quantity. I believe that I am not able to work with Pageimpl<> in the correct way, how to correct this situation? Below follows the method code of the Resource class responsible for calling the service method:
@GetMapping(value="/produtos/list")
public ResponseEntity<Page<ProdutoDTOVendedor>> listarProdutos(
@RequestParam(value = "descricao", defaultValue = "") String descricao,
@RequestParam(value = "disponivel", defaultValue = "") Boolean disponivel,
@RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "size", defaultValue = "24") Integer size,
@RequestParam(value = "orderBy", defaultValue = "descricao") String orderBy,
@RequestParam(value = "direction", defaultValue = "ASC") String direction) {
Page<ProdutoDTOVendedor> produtos = vendedorService.listarProdutos(descricao, disponivel, page, size, orderBy, direction);
return ResponseEntity.ok().body(produtos);
}
Marcelo talks, okay? Due to the time of the question, I changed the consultation approach to be able to work with pagination in this scenario. Before as I had described, using an approach through the keyword method (Keywords) in the Pository, so I switched to an approach using the Entitymanager and JPQL query in the service itself. I’ll put in the answer to what I did, but anyway thank you very much.
– Gonzaga Neto