Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

java - How to create a @RequestMapping @PathVariable to include a / in the parameter?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...