バリューREST @RequestMappingは、値に ‘. ‘が含まれていると正しく抽出されません.
問題
「http://localhost:8080/site/google.com」などのリクエストが送信された場合、Springは「
google
」を返します。ファイルの拡張子としてSpringの扱い “。”のように見え、パラメータ値の半分を抽出します。
SiteController.java
package com.mkyong.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/site") public class SiteController { @RequestMapping(value = "/{domain}", method = RequestMethod.GET) public String printWelcome(@PathVariable("domain") String domain, ModelMap model) { model.addAttribute("domain", domain); return "domain"; } }
解決策
これを修正するには、
@ RequestMapping`を正規表現にして、
。+
“を追加するようにしてください。今、Springは ”
google.com
“を返します。
SiteController.java
package com.mkyong.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/site") public class SiteController { @RequestMapping(value = "/{domain:.+}", method = RequestMethod.GET) public String printWelcome(@PathVariable("domain") String domain, ModelMap model) { model.addAttribute("domain", domain); return "domain"; } }
参考文献
MVC – 正規表現によるURIテンプレートパターン]。
http://en.wikipedia.org/wiki/Regular__expression
[正規表現
ウィキペディア]。
Spring jira – SPR-6164