使用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();
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想/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();
}
Run Code Online (Sandbox Code Playgroud)
然后/json?type=abc&track=aa或/json?track=rr将工作