I am using QueryDSL with Spring boot to retrieve records from database. I am able to sort the records by specifying the column name ("applicantid") like below:
@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);
}
But I want to sort by a parameter and pass it to the sort field (which can be applicantid, applicantname, etc - column fields). How to do it? I am not able to find proper syntax:
@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);
}
Only one column sorting is fine. If you can suggest with multiple column sorting that's great too. Please help me out. I am stuck at sorting. Thank you.