@RestController
@RequestMapping(value = "/players")
public class REST {
@RequestMapping(method = RequestMethod.GET)
public List<Player> getAll() {
return service.getAll();
}
@RequestMapping(method = RequestMethod.GET, params = {"team", "score"})
public List<Player> getByPlayerAndScore(@RequestParam(value = "team") String team,
@RequestParam(value = "score", required = false) int score) {
return service.getByPlayerAndScore(team, score);
}
}
Q1:我期待第一種方法爲URL「/玩家」(如預期工作)和第二種方法的工作,爲的URL(「/?玩家組隊= XYZ」的工作, 「/ players?team = xyz & score = 1000」)。 「/ players?team = xyz」使用spring1方法。即使我指定分數作爲可選項,除非我指定2個參數,春天是不使用第二種方法。如何解決這個問題,以及編寫控制器方法以處理這些類型的請求的最佳方式,用戶可以發送不同的可用參數集(如param1 & param2,僅param1,僅param2等)。彈簧安置@RequestMapping實行
Q2:對於使用不同參數集的第二種查詢類型,如何在DAO層中編寫數據庫查詢。我應該寫單獨的方法各有不同的查詢或一個方法有多個if語句(例如,如果用戶發送「團隊」添加團隊DB查詢,如果用戶發送「分數」將其添加到數據庫查詢...)
我給需要第二映射 –
=假@LokeshCherukuri你把在*參數*綁定,而不是請求映射。更新了答案。 – chrylis