zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    SpringBoot之集成slf4j日志框架
    Maven项目优势
    Idea操作技巧
    Nginx服务器之负载均衡策略(6种)
    Git操作规范
    Mybatis之Tk
    MyEclipse取消验证Js的两种方法
    文件异步上传,多文件上传插件uploadify
    EasyMock的使用
    jquery 中post 、get的同步问题,从外部获取返回数据
  • 原文地址:https://www.cnblogs.com/banning/p/6226147.html
Copyright © 2011-2022 走看看