为什么控制器中的@RequestMapping Spring注释会捕获更多我想要的?

时间:2015-05-18 15:50:47

标签: java spring spring-mvc

我有简单的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.errorhttp://localhost:18080/XXX/home.qwe123.234等链接。我没有为home.error或home.qwe123设置映射。 234等任何地方。我只在我的控制器中映射。 如何阻止控制器匹配?

1 个答案:

答案 0 :(得分:7)

因为默认情况下,Spring会使用PathMatchConfigurer设置useSuffixPatternMatch设置为true的MVC环境。来自javadoc

  

匹配模式时是否使用后缀模式匹配(".*")   要求。如果启用,映射到"/users"的方法也匹配   "/users.*"

     

默认值为true

您可以将false类扩展@Configuration并覆盖

,将其设置为MVC配置中的WebMvcConfigurationSupport
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
}