我有简单的Spring控制器和映射:
@Controller
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(HttpSession session, HttpServletRequest request, HttpServletResponse response, Model Principal principal) {
...
return "home";
}
}
它捕获http://localhost:18080/XXX/home
很自然,但为什么它会捕获http://localhost:18080/XXX/home.error
或http://localhost:18080/XXX/home.qwe123.234
等链接。我没有为home.error或home.qwe123设置映射。 234等任何地方。我只在我的控制器中映射。
如何阻止控制器匹配?
答案 0 :(得分:7)
因为默认情况下,Spring会使用PathMatchConfigurer
设置useSuffixPatternMatch
设置为true
的MVC环境。来自javadoc
匹配模式时是否使用后缀模式匹配
(".*")
要求。如果启用,映射到"/users"
的方法也匹配"/users.*"
。默认值为
true
。
您可以将false
类扩展@Configuration
并覆盖
WebMvcConfigurationSupport
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}