Spring MVC @PathVariable 最后一个点(dot)以后的字符串(或说扩展名)丢失

来源:互联网 发布:java导入项目中文乱码 编辑:程序博客网 时间:2024/05/01 20:23


转自:http://iteches.com/archives/7011


使用以下的 @PathVariable,当 key 值为 abc 或 123 时都没有问题。

@RequestMapping(value = "/release/{key}", method = RequestMethod.GET)public @ResponseBodyString release(@PathVariable String key) {    log.debug("取得key值 {}", key);    return release;}

但是当 key 值有「.」时就会出错,比如说「/release/a.b.c」,到了 @PathVariable 就只剩下「a.b」,「.c」不见了,原因出现 Spring MVC 预设会切掉最后一个点以后的字符串,应该是在处理「*.do」这样的 Url pattern 的关系。

解决方式:在 @PathVariable 里使用 Regular Expression 来配置 key 值的长相。

@RequestMapping(value = "/release/{key:[a-zA-Z0-9\\.]+}", method = RequestMethod.GET)public @ResponseBodyString release(@PathVariable String key) {    log.debug("取得key值 {}", key);    return release;}

另:Restful Springmvc详细介绍参考 http://blog.arganzheng.me/posts/restful-springmvc.html


0 0