如何创建@RequestMapping @PathVariable以在参数中包含/?

时间:2014-01-08 17:41:14

标签: java spring spring-mvc spring-el

在Spring 3.x中是否有办法在请求映射中包含一个PathVariable包含一个转发/?我已经尝试了不同的正则表达式,我认为它们已经正确解析了,但似乎他们从来没有设法拿起前锋/.// p>

我发现了this related SO问题,但这更多地依赖于参数的URL编码,这不是我的问题。

我尝试过以下@RequestMapping,但无济于事:

@RequestMapping(value = "/template/{definitionName:[a-zA-Z0-9_./]+}/{attributeName:.+}", method = RequestMethod.GET)
@RequestMapping(value = "/template/{definitionName}/{attributeName:[^/]+}", method = RequestMethod.GET)

例如,我正在尝试匹配以下网址:

http://localhost:8880/mustache/template/users/info/user_info.updateable

,其中

  • “users / info”将是definitionName
  • “user_info.updateable”将是attributeName

完整的方法原型将是:

  @RequestMapping(value = "/template/{definitionName:[a-zA-Z0-9_./]+}/{attributeName:.+}", method = RequestMethod.GET)
    public static void fetchTemplateDefinition(
            @PathVariable("definitionName") final String definitionName,
            @PathVariable("attributeName") final String attributeName,
            final HttpServletRequest request,
            final HttpServletResponse response) throws ServletException, IOException
    {...}

有没有办法匹配网址中包含/的参数?

1 个答案:

答案 0 :(得分:3)

开箱即用是不可能的。 Spring调用PathMatcher.extractUriTemplateVariables()来提取路径变量。 PathMatcher的默认实现是AntPathMatcher,并使用/作为分隔符将路径和路径模式拆分为多个部分。

唯一的解决方案是实现自己的PathMatcher(或扩展AntPathMatcher)并告诉Spring使用它。

相关问题