使用Spring 3.0,我可以有一個可選的路徑變量嗎?使用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
將工作
這無疑是做的一個很好的方式。 – Shamik 2011-02-05 01:48:00
@Shamik:在我看來,這是一個引人注目的理由*不*使用路徑變量。組合增殖可能很快失去控制。 – skaffman 2011-02-05 11:40:13
其實並不是因爲路徑不夠複雜,而是充滿了可選組件。如果您有多個或最多兩個可選路徑元素,則應認真考慮將其中的一些切換爲請求參數。 – 2012-04-23 10:44:40