问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/21002745/how-to-create-a-requestmapping-pathvariable-to-include-a-in-the-parameter