'List of possible values for particular path variable in Spring MVC
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 1:[1]
You can also use a enum as type for your @PathVariable that will make Spring to limit the accepeted request values to the values of the enum
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andreas Guther |