Spring MVC - @PathVariable point (.) Est tronqué
Examinez un exemple de Spring MVC@RequestMapping
et@PathVariable
.
@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"); } //...
Voir les cas suivants:
-
Pour l'entrée
/site/google
, «q» afficheragoogle
-
Pour l'entrée
/site/google.com
, «q» affichera toujoursgoogle
, le point (.) Est tronqué !? -
Pour l'entrée
/site/google.com.my
, «q» afficheragoogle.com
, le dernier point (.) Est tronqué! -
Pour l'entrée
/site/google.com.my.abc
, «q» afficheragoogle.com.my
-
Pour l'entrée
/site/cloud.google.com
, «q» afficheracloud.google
Le dernier point (.) Est toujours tronqué.
Note
Testé avec le ressort 3 et le ressort 4.
Solution
Pour résoudre ce problème, ajoutez un mappage regex{q:.+}
dans@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"); } //...
Maintenant, pour l'entrée/site/google.com
, «q» affichera lesgoogle
corrects