Spring-MVC RequestMapping URITemplate 中的可选路径变量
我有以下映射:
@RequestMapping(value = "/{first}/**/{last}", method = RequestMethod.GET)
public String test(@PathVariable("first") String first, @PathVariable("last")
String last) {}
对于以下 URI:
foo/a/b/c/d/e/f/g/h/bar
foo/a/bar
foo/bar
将 foo 映射到 first 并将 bar 映射到 last 并且有效美好的。
我想要的是将 foo 和 bar 之间的所有内容映射到单个路径参数中,如果没有中间则为 null(如最后一个 URI 示例):
@RequestMapping(value = "/{first}/{middle:[some regex here?]}/{last}",
method = RequestMethod.GET)
public String test(@PathVariable("first") String first, @PathVariable("middle")
String middle, @PathVariable("last") String last) {}
非常卡在正则表达式上,因为我希望像 {middle 这样简单的东西:.*},仅映射到 /foo/a/bar,或 {middle:(.*/)*},它似乎没有映射到任何内容。
在应用正则表达式模式之前,AntPathStringMatcher 是否对“/”进行标记? (使交叉/不可能的模式)或者有解决方案吗?
仅供参考,这是在 Spring 3.1M2 中,
这看起来与 @RequestMapping 控制器和动态 URL 类似,但是我在那里没有看到解决方案。
I have the following mapping:
@RequestMapping(value = "/{first}/**/{last}", method = RequestMethod.GET)
public String test(@PathVariable("first") String first, @PathVariable("last")
String last) {}
Which for the following URIs:
foo/a/b/c/d/e/f/g/h/bar
foo/a/bar
foo/bar
maps foo to first and bar to last and works fine.
What I would like is something that maps everything between foo and bar into a single path param, or null if there is no middle (as in the last URI example):
@RequestMapping(value = "/{first}/{middle:[some regex here?]}/{last}",
method = RequestMethod.GET)
public String test(@PathVariable("first") String first, @PathVariable("middle")
String middle, @PathVariable("last") String last) {}
Pretty stuck on the regex since I was hoping that something simple like {middle:.*}, which only maps to /foo/a/bar, or {middle:(.*/)*}, which seems to map to nothing.
Does the AntPathStringMatcher tokenize upon "/" prior to applying regex patterns? (making patterns that cross a / impossible) or is there a solution?
FYI this is in Spring 3.1M2
This seem similar to @RequestMapping controllers and dynamic URLs but I didn't see a solution there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我的项目中,我在 springframework 中使用内部变量:
请参阅 HandlerMapping.html#URI_TEMPLATE_VARIABLES_ATTRIBUTE
In my project, I use inner variable in the springframework:
See HandlerMapping.html#URI_TEMPLATE_VARIABLES_ATTRIBUTE
据我所知是做不到的。正如您所说,在每个斜杠处分割路径后,正则表达式将应用于路径元素,因此正则表达式永远不能匹配“/”。
您可以手动检查 url 并从请求对象中自行解析它。
Can't be done as far as I know. Just as you stated, the regular expression is being applied to the path element after splitting up the path at each slash, so the regular expression can never match a '/'.
You could manually inspect the url and parse it yourself from the request object.
这可以通过编写自定义路径匹配器并配置 Spring 使用它来完成。例如,这样的解决方案记录在此处: http://java.lang. dzone.com/articles/spring-3-webmvc-optional-path
该链接提供了一个自定义路径匹配器,并展示了如何配置 spring 来使用它。如果您不介意编写自定义组件,这应该可以解决您的需求。
另外,这是 With Spring 3.0 的副本,我可以创建一个可选的路径变量吗?
It could be done by writing a custom path matcher and configuring Spring to use it. For example, such a solution is documented here: http://java.dzone.com/articles/spring-3-webmvc-optional-path
The link provides a custom path matcher and shows how to configure spring to use it. That should solve your need if you don't mind writing a custom component.
Also, this is a duplicate of With Spring 3.0, can I make an optional path variable?
尝试使用
特定方法的可用 url 的数组列表,
但是当您在方法签名中使用 @PathVariable 时,创建 url 列表时要小心,它不能为 null。
希望这有帮助
try to use this
array list of available url for specific method
But be careful on creating list of url when you are using @PathVariable in your method signature it cant be null.
hope this help