SpringMVC 請求路徑結尾存在.使用@PathVariable訪問路徑內容,路徑參數獲取不准確的問題


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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM