[英]How to fix the error about PageRequest in Spring Boot?
我正在做我的項目。 然后我遇到了一個我無法處理的錯誤。 所以我的 Controller class 看起來像:
@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);
}
}
我在PageRequest
上有一個錯誤。 它說: .springframework.data.domain.PageRequest @Contract(value = "_,_,null->fail", pure = true) protected PageRequest(int page,int size, @NotNull org.springframework.data.domain.Sort sort
。所以我想不通。如何解決這個錯誤?
您需要將 Sort Object 傳遞給 PageRequest 構造函數。 問題是您的 PageRequest 構造函數缺少 Sort object 的參數
new PageRequest(page, 4)
相反,你應該有類似的東西 -
new PageRequest(page, 4, Sort.ascending())
嘗試使用PageRequest.of(...)
而不是構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.