---
title: "SpringMVC 请求路径结尾存在.使用@PathVariable访问路径内容，路径参数获取不准确的问题"
author: "caichenglong"
date: "2018-06-27T10:47:29Z"
tags: ["Spring"]
categories: ["开源软件"]
slug: "springmvc-pathvariable"
draft: false
lastmod: "2018-06-27T10:47:29Z"
id: 2425696
url: "https://caichenglong.iteye.com/blog/2425696"
---

SpringMVC采用Get方式请求资源时，如果请求路径的结尾中带有小数点(.)时，同时使用@PathVariable访问路径内容时，请求路径中最后一个小数点及其后面的内容会被Spring截断丢弃
比如针对版本的访问:
    对于请求路径：
        http://host:port/program/module/download/apk/3.20.10
    后端RequestMapping为
   

1 @RequestMapping(value="module/download/apk/{version}",method=RequestMethod.GET)
2 public void download(HttpSession session,HttpServletResponse response,@PathVariable("version")String version){
3     //解析后获得到的版本值为：3.20
4 }

 


又比如针对文件的访问
    对于请求路径：
        http://host:port/program/viewFile/module/201612201231445.pdf
    后端RequestMapping为
   

1 @RequestMapping(value="viewFile/{module}/{filename}",method=RequestMethod.GET)
2 public void viewFile(HttpSession session,HttpServletResponse response,@PathVariable String module, @PathVariable String filename){
3     //解析后获得到的文件名称为201612201231445并没有或追文件后缀
4 }

 


在确实需要使用以小数点的路径进行请求的话可以选择如下两种解决方案：
    1、在路径后加任意小数点结尾的字符串
        http://host:port/program/module/download/apk/3.20.10.html
        http://host:port/program/viewFile/module/201612201231445.pdf.jsp
    2、使用Spring正则表达式(SpEL)
       

1 @RequestMapping(value="module/download/apk/{version:.+}",method=RequestMethod.GET)
2 @RequestMapping(value="viewFile/{module}/{filename:.+}",method=RequestMethod.GET)

