java - 为什么 Controller 中的 @RequestMapping Spring 注释捕获了我想要的更多内容?

标签 java spring spring-mvc

我有带映射的简单 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这样的链接呢。 errorhttp://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/

相关文章:

java - 如何让玩家正确的移动到另外一个 field 呢?

java - 保持oracle连接存活,访问oracle

java - 如何在 Java 中克隆 MySQL 数据库

spring - 如何使用自定义注释 @Foo 找到所有 bean?

spring - 通过HttpServletResponse/HttpServletRequest获取HTML内容

java - 如何从 JBPM 工作流程中删除任务?

java - 在 IntegrationFlow 服务激活器方法成功返回之前不确认 RabbitMQ 消息?

spring - Spring @Configuration 类的问题

java - @ResponseStatus,客户端没有收到错误信息

java - @RequestBody在Spring中始终为空