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

kor*_*oll 3 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";
    }
}
Run Code Online (Sandbox Code Playgroud)

它捕获是很自然的http://localhost:18080/XXX/home,但为什么它会捕获像http://localhost:18080/XXX/home.errorhttp://localhost:18080/XXX/home.qwe123.234等等的链接.我没有为home.error或home.qwe123.234等设置映射.我只在我的控制器中映射.如何阻止控制器匹配?

Sot*_*lis 7

因为,默认情况下,Spring会将PathMatchConfigureruseSuffixPatternMatch设置为的MVC环境设置为true.来自javadoc

(".*")在将模式与请求匹配时是否使用后缀模式匹配.如果启用,则映射的方法"/users"也匹配 "/users.*".

默认值为true.

您可以false通过@Configuration扩展WebMvcConfigurationSupport和覆盖类来将其设置为MVC配置

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
}
Run Code Online (Sandbox Code Playgroud)