Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??
}
like image 806
Andremoniy Avatar asked Sep 16 '16 14:09

Andremoniy


Video Answer


1 Answers

It can be easily done using regular expressions in the mapping:

@RequestMapping("/persons/{searchField:id|code|email}/{searchValue}")
like image 157
Andremoniy Avatar answered Sep 24 '22 15:09

Andremoniy