我有带映射的简单 Spring Controller :
@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 等的映射。我只在我的 Controller 中有映射。
如何阻止 Controller 匹配它?
最佳答案
因为,默认情况下,Spring 使用 PathMatchConfigurer
设置您的 MVC 环境它的 useSuffixPatternMatch
设置为 true
。来自javadoc
Whether to use suffix pattern match
(".*")
when matching patterns to requests. If enabled a method mapped to"/users"
also matches to"/users.*"
.The default value is
true
.
您可以在您的 MVC 配置中将其设置为 false
,方法是让您的 @Configuration
类扩展 WebMvcConfigurationSupport
并覆盖
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
关于java - 为什么 Controller 中的 @RequestMapping Spring 注释捕获了我想要的更多内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30307678/