You can use the params element. One mapping will supports params="sort" for when the sort parameter is present and the other params="!sort" for when it is missing.
However, you may want to consider using a default value instead of performing a redirect. What benefit does the redirect provide? It will require the server respond and then and have the client make a second HTTP request.
Using params
@RestController
public class PersonController {
//only in case the "sort" query parameter is missing
@GetMapping(value = "/persons", params = "!sort")
public String unsorted() {
return "redirect:/persons?sort=name";
}
//only in case the "sort" query parameter exists
@GetMapping(value = "/persons", params = "sort")
public String sorted() {
//...
}
}
Using default value
@RestController
public class PersonController {
//only in case the "sort" query parameter exists
@GetMapping("/persons")
public String sorted(
@RequestParam(name = "sort", defaultValue = "name") String sort)
{
//...
}
}