javaspring-boot

No primary or default constructor found for interface org.springframework.data.domain.Pageable


I was trying to implement Pageable in my RestController and running into issues with this error message "No primary or default constructor found for interface org.springframework.data.domain.Pageable"

My Controller is

@GetMapping("/rest/category/all/page")
public Page<ItemCategory> getAllItemCategoryByPage(Pageable pageable){
    Page<ItemCategory> categories = itemCategoryService.getAllItemCategoriesByPageable(pageable);
    return categories;
}

What am I doing wrong here. It is a Spring Boot 2.0 Application. Thanks in advance!


Solution

  • I would suggest refactoring you controller to

    public List<ItemCategory> getAllItemCategoryByPage(@RequestParam("page") int pageIndex, 
                                                       @RequestParam("size") int pageSize){
         return itemCategoryService
                       .getAllItemCategoriesByPageable(PageRequest.of(pageIndex, pageSize)).getContent();
    }
    

    I think you are missing an annotation before Pageable (how are you sending that data from client?).