2010-09-22 61 views
0

設計我的RESTful API,因此我想用以下URISpring MVC:更正用於RESTful URI的控制器方法的註釋,包括';'

http://[HOST]/[PLANET]/[LAT];[LONG] 

例如

http://myserver/earth/50.2;29.1 

在Spring MVC中,這種方法的適當註釋是什麼?這是下一個好嗎?

@RequestMapping(value = "/{planet}/{lat};{long}", method = RequestMethod.GET) 
public String showInfoAboutCoords(
    @PathVariable final String planet, 
    @PathVariable final String lat, 
    @PathVariable final String long, 
    final HttpServletResponse response) { 
     // Implementation 
} 

如果這個人是好的 - 什麼是@MaskFormat("###-##-####")好?

回答

2

你的URI模式有兩個問題:

  • 一些servlet容器可能把;作爲分隔符,然後修整URI(例如Tomcat的bug 30535)。因此,作爲解決方法,您可以使用一些不同的字符,如,
  • 默認情況下,Spring MVC將URI中的點視爲擴展分隔符並對其進行修剪。您可以通過指定路徑變量的正則表達式模式來覆蓋它。

所以,你必須像

@RequestMapping(value = "/{planet}/{lat:.*},{long:.*}", method = RequestMethod.GET) 

注意,由於禁用了Spring的延伸處理,你有,如果你需要它(這還需要更嚴格的正則表達式,以避免混淆手動啓用與擴展分隔小數點):

@RequestMapping(value = 
    {"/{planet}/{lat:.*},{long:\\d+\\.\\d+}", 
     "/{planet}/{lat:.*},{long:\\d+\\.\\d+}.*"}, 
    method = RequestMethod.GET) 

通過@MaskFormat你可能從mvc-showcase意味着註釋(請注意,它的說明A內置註釋)。它與MaskFormatAnnotationFormatterFactory一起演示了將路徑變量(即字符串)轉換爲方法參數的新格式化功能。實際上它將String秒轉換爲String秒,所以它僅用於驗證。

+0

謝謝,它工作出色。是的,我指的是'mvc-showcase'的註釋。 – 2010-09-22 18:47:10

相關問題