3
考虑三个REST资源:名单
/persons/id/{id}
/persons/code/{code}
/persons/email/{email}
我想在Spring MVC
控制器的一个方法来处理这些请求:
@RequestMapping("/persons/{searchField}/{searchValue}")
public Person search(@PathVariable field, @PathVariable value) {
...
}
的问题是:是否有任何优雅的方式来限制searchField
的值的变量列表注释级别除了在方法search
中检查它们?
switch (field) {
case "id": ...
case "code": ...
...
default: // not found exception??
}
不要。分别定义三种适当路径的方法。 –
优雅的方法是为每个可接受的searchField值定义一个方法,正如Sotirios上面指出的那样 – holtc
@RequestMapping(“/ persons”)和使用RequestParam而不是PathVariable? – Aroniaina