On my app, pageable methods are run successfully and got Status 200 ok on Postman checking, however content is empty, and we can not see the list of adverts on Postman. We just have Status 200 ok and an empty pageable structure. here are my codes:
Controller:
@GetMapping
public Page<AdvertResponse> getAllAdvertsByPage(
@RequestParam(value = "q", required = false) String query,
@RequestParam(value = "category.id") Long categoryId,
@RequestParam(value = "advert_type_id") Long advertTypeId,
@RequestParam(value = "price_start", required = false) Double priceStart,
@RequestParam(value = "price_end", required = false) Double priceEnd,
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "20") int size,
@RequestParam(value = "sort", defaultValue = "category.id") String sort,
@RequestParam(value = "type", defaultValue = "asc") String type
) {
return advertService.getAllAdvertsByPage(query, categoryId, advertTypeId, priceStart, priceEnd, page, size, sort, type);
}
Service: public Page<AdvertResponse> getAllAdvertsByPage(String query, Long categoryId, Long advertTypeId, Double priceStart, Double priceEnd, int page, int size, String sort, String type) {
Pageable pageable = pageableHelper.getPageableWithProperties(page, size, sort, type);
if (methodHelper.priceControl(priceStart, priceEnd)) {
throw new ConflictException(ErrorMessages.START_PRICE_AND_END_PRICE_INVALID);
}
return advertRepository.findByAdvertByQuery(categoryId, advertTypeId, priceStart, priceEnd, query, pageable)
.map(advertMapper::mapAdvertToAdvertResponse);
}
and repo:
@Query("SELECT a FROM Advert a WHERE " +
"a.category.id = :categoryId AND " +
"a.advertType.id = :advertTypeId AND " +
"(:priceStart IS NULL OR :priceEnd IS NULL OR a.price BETWEEN :priceStart AND :priceEnd) AND " +
"(:query IS NULL OR LOWER(a.title) LIKE LOWER(CONCAT('%', :query, '%')) OR LOWER(a.description) LIKE LOWER(CONCAT('%', :query, '%')))")
Page<Advert> findByAdvertByQuery(@Param("categoryId") Long categoryId,
@Param("advertTypeId") Long advertTypeId,
@Param("priceStart") Double priceStart,
@Param("priceEnd") Double priceEnd,
@Param("query") String query,
Pageable pageable);
and this is my helper:
public Pageable getPageableWithProperties( int page, int size, String sort, String type) {
Pageable pageable=PageRequest.of(page,size, Sort.by(sort).ascending());
if(Objects.equals(type,"desc")){
pageable=PageRequest.of(page,size,Sort.by(sort).descending());
}
return pageable;
}
we checked our Query etc but we can not find where the problem is..
this is what I receive from Postman when run the app:
{
"content": [],
"pageable": {
"pageNumber": 1,
"pageSize": 10,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"offset": 10,
"paged": true,
"unpaged": false
},
"last": true,
"totalPages": 1,
"totalElements": 5,
"numberOfElements": 0,
"first": false,
"size": 10,
"number": 1,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"empty": true
}