![](/img/trans.png)
[英]Using a custom annotation on a Spring MVC controller method from an interceptor
[英]Spring MVC: Correct Annotation of Controller Method for RESTful URI including ';'
設計我的RESTful API時,我想使用以下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("###-##-####")
什么用?
您的URI模式有兩個問題:
;
作為分隔符並修整URI(例如Tomcat的錯誤30535 )。 所以,作為一種解決方法你可以使用一些不同的角色,比如,
。 所以,你會得到類似
@RequestMapping(value = "/{planet}/{lat:.*},{long:.*}", method = RequestMethod.GET)
請注意,由於您禁用了Spring的擴展處理,因此您必須在需要時手動啟用它(這還需要更多限制性的regexp以避免將小數點與擴展分隔符混淆):
@RequestMapping(value =
{"/{planet}/{lat:.*},{long:\\d+\\.\\d+}",
"/{planet}/{lat:.*},{long:\\d+\\.\\d+}.*"},
method = RequestMethod.GET)
@MaskFormat
可能表示來自mvc-showcase
的注釋(請注意,它是一個內置注釋)。 連同MaskFormatAnnotationFormatterFactory
一起,它演示了將路徑變量(即字符串)轉換為方法參數的新格式設置。 實際上,它會將String
轉換為String
,因此僅用於驗證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.