Spring MVC @PathVariable dot (.) get truncated
By:Roy.LiuLast updated:2019-08-17
Review a Spring MVC @RequestMapping and @PathVariable example.
@RequestMapping("/site") public class SiteController { @RequestMapping(value = "/{q}", method = RequestMethod.GET) public ModelAndView display(@PathVariable("q") String q) { logger.debug("Site : q : {}", q); return getModelAndView(q, "site"); //...
See the following cases :
- For input /site/google, “q” will display google
- For input /site/google.com, “q” will still display google, the dot (.) is truncated!?
- For input /site/google.com.my, “q” will display google.com, the last dot (.) is truncated!
- For input /site/google.com.my.abc, “q” will display google.com.my
- For input /site/cloud.google.com, “q” will display cloud.google
The last dot (.) is always getting truncated.
Note
Tested with Spring 3 and Spring 4.
Tested with Spring 3 and Spring 4.
Solution
To fix it, add a regex mapping {q:.+} in @RequestMapping
@RequestMapping("/site") public class SiteController { @RequestMapping(value = "/{q:.+}", method = RequestMethod.GET) public ModelAndView display(@PathVariable("q") String q) { logger.debug("Site : q : {}", q); return getModelAndView(q, "site"); //...
Now, for input /site/google.com, “q” will display the correct google
References
From:一号门
COMMENTS