使用Spring进行精确的RequestMapping [英] Exact RequestMapping with Spring
问题描述
我在春季有以下内容
@RequestMapping("/hello")
但是,Spring会自动为/hello/和/hello.*添加映射.如何完全匹配网址?
However Spring automatically adds mappings for /hello/ as well as /hello.*. How do I do an exact URL match?
仅/hello应该有效,其他应为404
Only /hello should work, anything else should 404
推荐答案
关闭RequestMappingHandlerMapping
上的后缀匹配(useSuffixPatternMatch
)将解决您的问题,但是,如果使用<mvc:annotation-driven/>
,实际上并不是那么容易在您的配置中(而不是手动布线所有必需的基础结构bean).在这种情况下,定义其他类型为RequestMappingHandlerMapping
的bean不会产生任何作用.
Turning off suffix matching (useSuffixPatternMatch
) on RequestMappingHandlerMapping
will solve your problem, but doing so is actually not so easy, if you use <mvc:annotation-driven/>
in your configuration (instead of manually wiring all the necessary infrastructure beans). In this case defining an additional bean of type RequestMappingHandlerMapping
won't have any effect.
您有两个选择:
-
删除
<mvc:annotation-driven/>
,将其扩展为等效的bean定义集,您可以在其中应用useSuffixPatternMatch
设置.
Remove
<mvc:annotation-driven/>
expanding it to an equivalent set of bean definitions where you can apply theuseSuffixPatternMatch
setting.
保持现状<mvc:annotation-driven/>
,并使用此处描述的更简单的解决方法: https://jira.springsource.org/browse/SPR-9371 .基本上,这会添加一个BeanPostProcessor
来检索由mvc命名空间创建的RequestMappingHandlerMapping
bean,并设置上述标记.
Keep <mvc:annotation-driven/>
as it is, and use a much easier workaround described here: https://jira.springsource.org/browse/SPR-9371. This basically adds a BeanPostProcessor
which retrieves the RequestMappingHandlerMapping
bean created by the mvc namespace, and sets the above mentioned flag.
还有另一张票据,它要求自定义RequestMappingHandlerMapping
是由mvc命名空间创建的,而没有像上面那样应用hack.您可以考虑对此票进行投票.
There is also another ticket requesting that it should be much easier to customize the RequestMappingHandlerMapping
created by the mvc namespace without applying hacks like above. You can consider voting on this ticket.
这篇关于使用Spring进行精确的RequestMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!