Spring请求映射通配符异常 [英] Spring request mapping wildcard exceptions
问题描述
我可以将/**通配符放在请求映射的中间,例如:"/some/resource/**/somthing"
Can I put /** wildcard in a middle of request mapping such as: "/some/resource/**/somthing"
我可以在春季3做到这一点
In Spring 3 I can do this
@RequestMapping("/some/resource/**")
要映射
/some/resource/A -> ControllerMethod1
/some/resource/A/B -> ControllerMethod1
/some/resource/A/B/C/D/E/F -> ControllerMethod1
用于任意数量的路径部分
for any number of paths parts
但是此映射过于贪婪,不允许我将子URL @RequestMapping("/some/resource/**/somthing")
映射到另一个控制器,例如
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
/some/resource/A/somthing -> ControllerMethod2
/some/resource/A/B/somthing -> ControllerMethod2
/some/resource/A/B/C/D/E/F/somthing -> ControllerMethod2
我该怎么做?
推荐答案
我认为无法按需要在URL映射中使用该ant样式,因为它将停止在下一个路径分隔符'/".
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 '/'.
I would suggest you to try 16.3.2.2. URI Template Patterns with Regular Expressions in order to map just the last part of the request (haven't tried this approach yet).
您还可以使用 PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE 匹配其余的请求,并在其中应用一些表达式. 查看此信息.
Also you can match the rest of the request using PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, and apply some expression there. Check this post.
否则,您应该使用请求参数来匹配该条件 16.3.2.6.请求参数和标题值.
Otherwise you should use request parameters to match that condition 16.3.2.6. Request Parameters and Header Values.
您可以通过"myParam",!myParam"或"myParam = myValue"之类的请求参数条件来缩小请求匹配范围.前两次测试是否存在请求参数,第三次测试特定参数值.这是一个带有请求参数值条件的示例.
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.
在这种情况下,您将使用params映射类似的内容
In this case you will map something like that using params
@RequestMapping(value = {"/some/resource/**"}, params="somthing")
或在方法签名中使用带有非必需属性的注释请求参数:
or use the annotation request parameter with not required attribute in method signature:
public void test(@RequestParam(value = "somthing", required=false) String str) {
这篇关于Spring请求映射通配符异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!