代码之家  ›  专栏  ›  技术社区  ›  XZen

Spring MVC,模式在路径变量中出现正斜杠符号时无法正常工作

  •  0
  • XZen  · 技术社区  · 11 年前

    我的控制器中有这样的方法:

    @RequestMapping("/action/{actionType:root/testaction}")
    public String test( @PathVariable String actionType) {        
    }
    

    当我收到带有url的请求时 localhost/action/root/testaction -不调用此方法。 但当我改变方法时,如:

    @RequestMapping("/action/root/{actionType:testaction}")
    public String test( @PathVariable String actionType) {        
    }
    

    做同样的get请求-方法也可以。问题是我想在PathVariable字符串中包含“/root”部分。

    你能澄清一下我的代码中有什么错误吗?

    1 回复  |  直到 11 年前
        1
  •  1
  •   Community Mohan Dere    4 年前

    问题是路径变量可能不包含“/”字符,因为这会使解析更加困难。

    春天 @RequestMapping 功能部分实现 RFC 6570: URI Template 。该标准不允许“/” characters in variable names :

     variable-list =  varspec *( "," varspec )
     varspec       =  varname [ modifier-level4 ]
     varname       =  varchar *( ["."] varchar )
     varchar       =  ALPHA / DIGIT / "_" / pct-encoded
    
     modifier-level4 =  prefix / explode
     prefix        =  ":" max-length
     max-length    =  %x31-39 0*3DIGIT   ; positive integer < 10000
     explode       =  "*"