首页 > 解决方案 > Spring Boot 2.2.0.RELEASE 不映射 thymeleaf 放置和删除请求

问题描述

从 Spring Boot 2.1.6.RELEASE 升级到 2.2.0.RELEASE 后,我基于 Thymeleaf 的页面在嵌入式 Tomcat 上执行放置和删除时失败。获取和发布工作正常。

我在 Chrome 开发人员工具中看到正在发送以下请求:

请求网址:https://localhost:8443/notifications/1 请求方法:POST

请求正文包含 _method="put" 和 _csrf 令牌。

有趣的是,我使用 @SpringBootTest 注释的集成测试正在通过。

Spring Boot Actuator 显示 /notifications/{notificationId} 映射到 PUT。

切换回 2.1.6.RELEASE 解决了这个问题。

我的 Thymeleaf 形式定义如下:

<form id="notificationForm" th:action="@{/notifications/{notificationId}(notificationId=${notification.id})}" th:object="${notification}" th:method="put" class="needs-validation" novalidate autocomplete="off">

我的控制器方法注释如下:

  @PutMapping("/notifications/{notificationId}")
  public String updateNotification(@PathVariable("notificationId") final Long notificationId,
      @Valid @ModelAttribute(name = NOTIFICATION_MODEL_ATTRIBUTE) final NotificationDto notificationDto,
      final BindingResult result, final Model model, final RedirectAttributes attributes) 

当我执行删除时,控制台中会显示以下堆栈跟踪:

org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
    at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:201)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:421)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:367)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getHandlerInternal(RequestMappingHandlerMapping.java:449)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getHandlerInternal(RequestMappingHandlerMapping.java:67)
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:393)
    at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1234)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1016)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
...

好像 Spring 在解析映射之前没有使用 _method 执行必要的方法转换。

有没有人有任何想法?

标签: spring-bootthymeleaf

解决方案


好像 Spring 在解析映射之前没有使用 _method 执行必要的方法转换。

这正是正在发生的事情。此转换由 Spring Boot 2.2 执行HiddenHttpMethodFilter默认情况下禁用

现在默认禁用处理请求参数的过滤器,_method因为如果请求主体可能包含参数,它会导致请求主体的早期消耗。这可以通过设置spring.webflux.hiddenmethod.filter.enabled或设置spring.mvc.hiddenmethod.filter.enabled为 true 来恢复。

您正在使用 Spring MVC,因此您应该设置spring.mvc.hiddenmethod.filter.enabled=true.


推荐阅读