2015-09-01 34 views
2

我想創建一個重載的控制器方法在春季過濾,允許多個請求參數。我在這裏找到的一些答案通過識別傳入的註釋中的請求參數提供了一個解決方案,但是,這不足以滿足我的需求,因爲我接受多個參數進行過濾並且不想聲明所有這些都是要求的參數。重載彈簧控制器方法獲取與變量請求參數

下面是一個例子:

@RequestMapping(method=RequestMethod.GET) 
public List<JSONRepresentation> getJSONRepresentation( 
    @RequestParam Map<String, String> filterParams) throws ObjectNotFoundException 
    { 
     // DO STUFF AND FILTER ON VALID PARAMETERS. 
    } 

@RequestMapping(method = RequestMethod.GET) 
public List<JSONRepresentation> getJSONRepresentation() throws ObjectNotFoundException 
    { 
     // DO STUFF AND RETURN ALL. 
    } 

試圖執行此操作而無需像「/過濾器」一額外的價值添加到URL

回答

3

實際上,這是不可能這樣的。你可以做什麼它的設置filterParams爲可選,做你的邏輯,每一種情況下:

@RequestMapping(method=RequestMethod.GET) 
public List<JSONRepresentation> getJSONRepresentation(@RequestParam(required=false) Map<String, String> filterParams) throws ObjectNotFoundException 
{ 
    if(filterParams != null) { 
    // DO STUFF AND FILTER ON VALID PARAMETERS. 
    } else { 
    // DO STUFF AND RETURN ALL. 
    } 

} 
+1

啊,就這麼簡單這麼容易,非常感謝你。 – pipedreams2

+0

hmm ,,貌似我說得太早了,filterParams即使沒有包含也不會評估爲空... – pipedreams2

+0

更改爲檢查是否(!filterParams.isEmpty())問題解決了 – pipedreams2