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

标签 java spring spring-mvc spring-el

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/

相关文章:

需要 Spring 面试问题

java - EhCache + hibernate

java - RESTful webservice与spring mvc上传图片集成测试

java - 是否有一种简单而干净的方法将表单输入绑定(bind)到域类上的两个属性?

java - 使用 Hibernate 配置 Spring。创建名称为 'usersDao' : Unsatisfied dependency expressed through field 'sessionFactory' 的 bean 时出错

java - Ant 编译但不运行或创建 jar

java - 配置 Hibernate 以从连接池获取新连接

java - 不支持内容类型 'multipart/form-data;boundary=----...;charset=UTF-8'

spring - 在 Spring MVC 中管理自定义 Accept header

java - Hibernate 的 load() 方法对不存在的 ID 做了什么?