我可以将/** 通配符放在请求映射的中间,例如:“/some/resource/**/somthing”
在 Spring 3 中我可以做到这一点
@RequestMapping("/some/resource/**")
映射
/some/resource/A -> ControllerMethod1
/some/resource/A/B -> ControllerMethod1
/some/resource/A/B/C/D/E/F -> ControllerMethod1
对于任意数量的路径部分
但是这个映射太贪心了,不允许我将子 URL @RequestMapping("/some/resource/**/somthing")
映射到另一个 Controller ,例如
/some/resource/A/somthing -> ControllerMethod2
/some/resource/A/B/somthing -> ControllerMethod2
/some/resource/A/B/C/D/E/F/somthing -> ControllerMethod2
我该怎么做?
最佳答案
我认为不可能按照您的要求在 url 映射中使用那种 ant 样式,因为它会在下一个路径分隔符“/”处停止。
我建议你试试16.3.2.2. URI Template Patterns with Regular Expressions为了映射请求的最后一部分(还没有尝试过这种方法)。
您还可以使用 PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE 匹配请求的其余部分,并在其中应用一些表达式。 Check this post.
否则您应该使用请求参数来匹配该条件16.3.2.6. Request Parameters and Header Values .
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.
在这种情况下,您将使用参数映射类似的东西
@RequestMapping(value = {"/some/resource/**"}, params="somthing")
或者在方法签名中使用不需要属性的注解请求参数:
public void test(@RequestParam(value = "somthing", required=false) String str) {
关于Spring 请求映射通配符异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19981012/