Issue
Consider three REST resources:
/persons/id/{id}
/persons/code/{code}
/persons/email/{email}
I want to make one method in a Spring MVC
controller to handle these requests:
@RequestMapping("/persons/{searchField}/{searchValue}")
public Person search(@PathVariable field, @PathVariable value) {
...
}
The question: is there any elegant way to restrict searchField
's variable list of values on the annotation level except just by checking them in the method search
?
switch (field) {
case "id": ...
case "code": ...
...
default: // not found exception??
}
Solution
It can be easily done using regular expressions in the mapping:
@RequestMapping("/persons/{searchField:id|code|email}/{searchValue}")
Answered By - Andremoniy
Answer Checked By - Marie Seifert (JavaFixing Admin)