一覧

2016-09-16 17 views
3

は3つのRESTリソースを考えてみましょう:一覧

/persons/id/{id} 
/persons/code/{code} 
/persons/email/{email} 

私はこれらの要求を処理するためにSpring MVCコントローラで一つの方法を作りたい:

@RequestMapping("/persons/{searchField}/{searchValue}") 
public Person search(@PathVariable field, @PathVariable value) { 
... 
} 

質問です:の値リスト変数を注釈レベルに制限するエレガントな方法は、方法searchでチェックするだけですか?

switch (field) { 
    case "id": ... 
    case "code": ... 
    ... 
    default: // not found exception?? 
} 
+3

しないでください。適切なパスを持つ3つのメソッドをそれぞれ定義します。 –

+0

エレガントな方法は、Sotiriosが上記の – holtc

+0

で述べたように、searchFieldの許容値ごとにメソッドを定義することです。@RequestMapping( "/ persons")とPathVariableではなくRequestParamを使用しますか? – Aroniaina

答えて

6
それは簡単にマッピングで regular expressionを使用して行うことができ

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

あなたはまた、春はの値にaccepeted要求値を制限するようになりますあなたの@PathVariableの型として列挙型を使用することができます列挙型