Spring 3.x 中有没有办法让请求映射中的 PathVariable 包含转发/?我尝试了不同的正则表达式,我认为它们可以正确解析,但它们似乎从未设法获取正向/。
我找到了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)
例如,我尝试匹配以下 URL:
http://localhost:8880/mustache/template/users/info/user_info.updateable
哪里
- “users/info”将是定义名称
- “user_info.updateable”将是 attributeName
完整的方法原型(prototype)是:
@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
{...}
有什么办法可以匹配URL中包含/的参数吗?
最佳答案
开箱即用是不可能的。 Spring 调用 PathMatcher.extractUriTemplateVariables() 来提取路径变量。 PathMatcher
的默认实现是 AntPathMatcher
,它使用 /
作为分隔符将路径和路径模式分成几部分。
唯一的解决方案是实现您自己的 PathMatcher(或扩展 AntPathMatcher)并告诉 Spring 使用它。
关于java - 如何创建 @RequestMapping @PathVariable 以在参数中包含/?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21002745/