![](/img/trans.png)
[英]Has problems after @PathVariable parameter value containing the '#' character,with Spring Boot
[英]spring boot sort by parameter or pathvariable
我在Spring Boot上使用QueryDSL從數據庫檢索記錄。 我可以通過指定列名(“ applicantid”)對記錄進行排序,如下所示:
@GetMapping("/applicants")
@ResponseBody
public Iterable<Applicant> getAllApplicants(@QuerydslPredicate(root = Applicant.class) Predicate predicate, @PageableDefault(sort = { "applicantid"}, value = 25) Pageable pageable) {
return this.applicantService.getAllApplicants(predicate, pageable);
}
但是我想按參數排序,並將其傳遞到排序字段(可以是Applicatorid,ApplicantName等-列字段)。 怎么做? 我找不到正確的語法:
@GetMapping("/applicants/{sortBy}")
@ResponseBody
public Iterable<Applicant> getAllApplicants(@QuerydslPredicate(root = Applicant.class) Predicate predicate, @PathVariable(value = "sortBy") String sortBy
@PageableDefault(sort = sortBy, value = 25) Pageable pageable) {
return this.applicantService.getAllApplicants(predicate, pageable);
}
僅一列排序就可以了。 如果您可以建議使用多列排序,那也很好。 請幫幫我。 我無法進行排序。 謝謝。
您正在設置要排序的屬性
@PageableDefault(sort = sortBy, value = 25) Pageable pageable) . . .
而不是自行設置,而是要求客戶端發送要排序的參數。 因此,您的請求將如下所示:
http://localhost:8080/applicants?sort=applicantId&applicantId.dir=desc&size=25
這等效於Pageable(sort = applicantId, Order = SortOrder.DESC, value =25)
您還可以傳遞多個排序參數。
如果您明確希望控制排序參數,則可以執行以下操作:
@GetMapping("/applicants/{sortBy}")
@ResponseBody
public Iterable<Applicant> getAllApplicants(@QuerydslPredicate(root = Applicant.class) Predicate predicate,
@PathVariable(value = "sortBy") String sortBy,
Pageable pageable) {
Sort sort = pageable.getSort();
if (sort != null) {
sort = new Sort(sortBy, "other params");
}
pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
return this.applicantService.getAllApplicants(predicate, pageable);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.