@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