名单

2016-09-16 68 views
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?? 
} 
+3

不要。分别定义三种适当路径的方法。 –

+0

优雅的方法是为每个可接受的searchField值定义一个方法,正如Sotirios上面指出的那样 – holtc

+0

@RequestMapping(“/ persons”)和使用RequestParam而不是PathVariable? – Aroniaina

回答

6

它可以映射使用regular expression很容易做到:

@RequestMapping("/persons/{searchField:id|code|email}/{searchValue}") 
0

你也可以使用一个枚举类型为你@PathVariable,这将使春天到accepeted请求的值限制的值枚举