I am trying to write an endpoint that allows the use of optional sorters in the backend.
For example, I have a sorter that allows me to sort elements in a list - which is what my endpoint's controller returns - based on their creation date.
If the corresponding controller parameter is true
then the elements are sorted started from the newest and moving towards the oldest.
If the corresponding parameter is false
, then the opposite.
This is a Spring Boot project.
I was wondering if there is a more spring-appropriate way to achieve this?
This is my controller:
@RestController
public class StudentsController{
@Autowired
private GradeBook yearlyGrades;
@GetMapping("/successful")
public List<Student> getSuccessfulStudents(
@RequestParam(name = "startDate") Instant startDate,
@RequestParam(name = "endDate") Instant endDate,
@RequestParam(defaultValue = "false", required = false) boolean sortStartingFromHighestGrade,
@RequestParam(defaultValue = "false", required = false) boolean sortStartingFromEarliestDate) {
return this.yearlyGrades.getSuccessfulStudents(startDate, endDate,
sortStartingFromHighestGrade,
sortStartingFromEarliestDate);
}
}
Depending on the true/false
value of the last two parameters:
sortStartingFromHighestGrade
sortStartingFromEarliestDate
Different processing happens in the background in the service class.