美文网首页
spring mvc 把 @PathVariable 把最后一个

spring mvc 把 @PathVariable 把最后一个

作者: 李健同学 | 来源:发表于2016-08-17 14:37 被阅读0次

今天在使用@PathVariable 注解接受参数的时候,传递了一个版本号2.2.0,结果发现后台中接收到的值为2.2,原因是因为Spring MVC 预设会切掉最后一个点以后的字符串,应该是在处理「*.do」这样的 Url pattern 的关系。

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

出错代码:
@RequestMapping(value = { "/versions/{version}" }, method = RequestMethod.DELETE) @ResponseBody public GeneralResult deleteVersion(@PathVariable String version,AppVersionForm form) throws ServiceException { ... }

改后代码:
@RequestMapping(value = { "/versions/{version:[a-zA-Z0-9\\.]+}" }, method = RequestMethod.DELETE) @ResponseBody public GeneralResult deleteVersion(@PathVariable String version,AppVersionForm form) throws ServiceException { ... }

相关文章

网友评论

      本文标题:spring mvc 把 @PathVariable 把最后一个

      本文链接:https://www.haomeiwen.com/subject/cppmsttx.html