spring Spring请求映射通配符异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19981012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring request mapping wildcard exceptions
提问by Rob McFeely
Can I put /** wildcard in a middle of request mapping such as: "/some/resource/**/somthing"
我可以将 /** 通配符放在请求映射的中间,例如:“/some/resource/**/somthing”
In Spring 3 I can do this
在 Spring 3 我可以做到这一点
@RequestMapping("/some/resource/**")
to map
映射
/some/resource/A -> ControllerMethod1
/some/resource/A/B -> ControllerMethod1
/some/resource/A/B/C/D/E/F -> ControllerMethod1
for any number of paths parts
对于任意数量的路径部分
However this mapping is too greedy and will not allow me to map a sub URL @RequestMapping("/some/resource/**/somthing")
to another controller such as
但是这种映射过于贪婪,不允许我将子 URL 映射@RequestMapping("/some/resource/**/somthing")
到另一个控制器,例如
/some/resource/A/somthing -> ControllerMethod2
/some/resource/A/B/somthing -> ControllerMethod2
/some/resource/A/B/C/D/E/F/somthing -> ControllerMethod2
How can i do this?
我怎样才能做到这一点?
采纳答案by jmventar
I thinks it's not possible to use that ant style in url mapping as you require, because it will stop on the next path separator character '/'.
我认为不可能根据需要在 url 映射中使用该 ant 样式,因为它会在下一个路径分隔符 ' /' 处停止。
I would suggest you to try 16.3.2.2. URI Template Patterns with Regular Expressionsin order to map just the last part of the request (haven't tried this approach yet).
我建议您尝试16.3.2.2。带有正则表达式的 URI 模板模式,以便仅映射请求的最后一部分(尚未尝试过这种方法)。
Also you can match the rest of the request using PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, and apply some expression there. Check this post.
您也可以使用PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE匹配请求的其余部分,并在那里应用一些表达式。检查这个帖子。
Otherwise you should use request parameters to match that condition 16.3.2.6. Request Parameters and Header Values.
否则,您应该使用请求参数来匹配该条件16.3.2.6。请求参数和标头值。
You can narrow request matching through request parameter conditions such as "myParam", "!myParam", or "myParam=myValue". The first two test for request parameter presense/absence and the third for a specific parameter value. Here is an example with a request parameter value condition.
您可以通过“myParam”、“!myParam”或“myParam=myValue”等请求参数条件来缩小请求匹配范围。前两个测试请求参数存在/不存在,第三个测试特定参数值。这是一个带有请求参数值条件的示例。
In this case you will map something like that using params
在这种情况下,您将使用 params 映射类似的内容
@RequestMapping(value = {"/some/resource/**"}, params="somthing")
or use the annotation request parameter with not required attribute in method signature:
或者在方法签名中使用带有 not required 属性的注释请求参数:
public void test(@RequestParam(value = "somthing", required=false) String str) {