名單

2016-09-16 62 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請求的值限制的值枚舉