【发布时间】:2011-06-21 16:54:37
【问题描述】:
在 Spring 3.0 中,我可以有一个可选的路径变量吗?
例如
@RequestMapping(value = "/json/{type}", method = RequestMethod.GET)
public @ResponseBody TestBean testAjax(
HttpServletRequest req,
@PathVariable String type,
@RequestParam("track") String track) {
return new TestBean();
}
在这里我希望/json/abc 或/json 调用相同的方法。
一种明显的解决方法是将type 声明为请求参数:
@RequestMapping(value = "/json", method = RequestMethod.GET)
public @ResponseBody TestBean testAjax(
HttpServletRequest req,
@RequestParam(value = "type", required = false) String type,
@RequestParam("track") String track) {
return new TestBean();
}
然后/json?type=abc&track=aa 或/json?track=rr 将起作用
【问题讨论】: