spring-boot - 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 在解析映射之前没有使用 _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
.
推荐阅读
- c# - c#替换CSV中的标题文本
- android - 前台工作人员中的多个通知
- application-verifier - 忽略 AppVerifier 中的 INVALID HANDLE 错误?
- azure - 如何使用逻辑应用从服务总线主题收集多条消息
- sharepoint - 通过 Microsoft Graph API 从 SharePoint 获取所有网站
- shader - 来自纹理图集的 WebGL 平铺
- wordpress - 高级自定义字段中继器上的分页停止工作
- android - 使用 AndroidKeyStore 计算数字签名
- faunadb - FaunaDB:如何获取最近一小时内创建的文档?
- php - 内容有 Html 标签时如何显示限制内容