加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 为什么控制器中的@RequestMapping Spring注释会捕获更多

发布时间:2020-12-15 04:27:42 所属栏目:Java 来源:网络整理
导读:我有简单的 Spring控制器与映射: @Controllerpublic class HomeController {@RequestMapping(value = "/home",method = RequestMethod.GET) public String home(HttpSession session,HttpServletRequest request,HttpServletResponse response,Model Princi
我有简单的 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等的映射.我只在我的控制器中映射.
如何阻止控制器匹配?

解决方法

因为,默认情况下,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.

通过让@Configuration类扩展WebMvcConfigurationSupport并覆盖,可以在MVC配置中将其设置为false

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读