Spring 请求映射通配符异常

标签 spring url spring-mvc

我可以将/** 通配符放在请求映射的中间,例如:“/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/

相关文章:

java - 使用 @Preauthorize 多个 Controller 进行注释时出现 Spring Security 错误

java - InsufficientAuthenticationException : There is no client authentication. 尝试添加适当的身份验证过滤器

java - 带有附加参数的 Spring 前进?

java - 业务逻辑数据无法使用 Spring Batch(和 Spring-Admin)持久保存

java - @DataJpaTest 无法排除响应式(Reactive) mongo 存储库

java - 如何从#后的地址中获取值?

c# - 使用 System.Uri 删除多余的斜线

html - IE 7 & 8 中的 URL 参数双重编码问题

java - hibernate : Using AND combined with OR in a single query

css - 如何在jsp文件中添加css文件