Asell
Asell

Reputation: 303

How to fix the error about PageRequest in Spring Boot?

I was working on my project. Then I had an error which I can't handle it. So my Controller class look like:

@Controller
public class CountryController {

    @Autowired
    private CountryRepository countryRepo;

    @GetMapping("/")
    public String showPage(Model model, @RequestParam(defaultValue = "0") int page){
        model.addAttribute("data", countryRepo.
                findAll(new PageRequest(page, 4)));
        return "index";
    }

    public String save(Country c){
        countryRepo.save(c);
    }

}

I have an error on PageRequest . It says: .springframework.data.domain.PageRequest @Contract(value = "_,_,null->fail", pure = true) protected PageRequest(int page,int size, @NotNull org.springframework.data.domain.Sort sort. So I couldn't figure out it. How to fix that error?

Upvotes: 1

Views: 1528

Answers (2)

IKo
IKo

Reputation: 5796

Try to use PageRequest.of(...) instead of constructor.

Upvotes: 1

SKumar
SKumar

Reputation: 2030

You need to pass a Sort Object to the PageRequest Constructor. The problem is that your PageRequest constructor is missing a parameter for Sort object

new PageRequest(page, 4)

Instead you should have something like -

new PageRequest(page, 4, Sort.ascending())

Upvotes: 2

Related Questions