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