0

Is there a way in Spring 3.x to have a PathVariable in a request mapping include a forward /? I've tried different regexs that I thought would have parsed properly, but it seems that they never manage to pick up the forward /.

I found this related SO question, but that is more dependent on URL encoding of parameters, which is not exactly my problem.

I've tried the following @RequestMapping but to no avail:

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

For example, I am trying to match the following URLs:

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

where

  • "users/info" would be definitionName
  • "user_info.updateable" would be attributeName

The full method prototype would be:

  @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
    {...}

Is there any way to match parameters that contain a / in the URL?

Community
  • 1
  • 1
Eric B.
  • 23,425
  • 50
  • 169
  • 316
  • Your first regex will fail because . matches any character and regexes are "greedy" by default so the `definitionName` part will match `users/info/user_info.updateable` with nothing left for the `attributeName` – Taylor Jan 08 '14 at 18:11
  • @Taylor Actually, '.' in a character set is supposed to be the literal '.' character; not the wildcard. But that still does not explain why the second regex does not work. – Eric B. Jan 08 '14 at 18:33
  • Your intent may be the literal . character but unless you escape it, that's not what it is. – Taylor Jan 08 '14 at 19:24
  • 1
    @Taylor - Are you sure about that? To my knowledge, Regex character sets are literal - hence [.] would be the '.' character and not the wildcard. – Eric B. Jan 08 '14 at 19:49

1 Answers1

3

It's not possible out of the box. Spring calls PathMatcher.extractUriTemplateVariables() to extract the path variables. The default implementation of PathMatcher is AntPathMatcher and that splits the path and the path pattern into pieces using /as a separator.

The only solution would be to implement your own PathMatcher (or extend the AntPathMatcher) and tell Spring to use it.

a better oliver
  • 26,330
  • 2
  • 58
  • 66
  • Thanks for the tip. Any idea how one configures the PathMatcher in Spring 3.2? `AnnotationMethodHandlerAdapter` has been deprecated in exchanged for `RequestMappingHandlerAdapter` which does not have the `PathMatcher`. The only place I found it was in the `WebContentInterceptor`. Is that the correct place? How does one configure the `WebContentInterceptor`? – Eric B. Jan 09 '14 at 04:52
  • @EricB. `RequestMappingHandlerMapping` – a better oliver Jan 09 '14 at 11:32
  • Am I missing something? I don't see it anywhere in the RequestMappingHandlerMapping to set the PathMatcher. – Eric B. Jan 09 '14 at 15:11
  • @EricB. The method itself is defined in the abstract base class `AbstractHandlerMapping` – a better oliver Jan 09 '14 at 18:16