简体   繁体   English

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

[英]Why does @RequestMapping Spring annotation in controller capture more that I want?

I have simple Spring controller with mapping: 我有简单的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";
    }
}

It is natural that it capture http://localhost:18080/XXX/home , but why does it capture links like http://localhost:18080/XXX/home.error or http://localhost:18080/XXX/home.qwe123.234 etc. I haven't set mapping for home.error or home.qwe123.234 etc. anywhere. 它很自然地捕获http://localhost:18080/XXX/home ,但为什么它会捕获http://localhost:18080/XXX/home.errorhttp://localhost:18080/XXX/home.qwe123.234 http://localhost:18080/XXX/home.error http://localhost:18080/XXX/home.qwe123.234等我没有在家里设置home.error或home.qwe123.234等的映射。 I have mapping only in my controllers. 我只在我的控制器中映射。 How to stop controller to matching that? 如何阻止控制器匹配?

Because, by default, Spring sets up your MVC environment with a PathMatchConfigurer which has its useSuffixPatternMatch set to true . 因为,默认情况下,Spring使用PathMatchConfigurer设置MVC环境,该设置将useSuffixPatternMatch设置为true From the javadoc 来自javadoc

Whether to use suffix pattern match (".*") when matching patterns to requests. 在将模式与请求匹配时是否使用后缀模式匹配(".*") If enabled a method mapped to "/users" also matches to "/users.*" . 如果启用,映射到"/users"也匹配"/users.*"

The default value is true . 默认值为true

You can set it to false in your MVC configuration by having your @Configuration class extend WebMvcConfigurationSupport and overriding 通过让@Configuration类扩展WebMvcConfigurationSupport并覆盖,可以在MVC配置中将其设置为false

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM