从 Spring Boot 2.1.6.RELEASE 升级到 2.2.0.RELEASE 后,我的基于 Thymeleaf 的页面在嵌入式 Tomcat 上执行放置和删除时失败。获取和发布工作正常。
我在 Chrome 开发者工具中看到正在发送以下请求:
请求网址:https://localhost:8443/notifications/1 请求方式:POST
请求正文同时包含 _method="put"和 _csrf token 。
有趣的是,我的带有@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">
我的 Controller 方法注释如下:
@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 执行必要的方法转换。
有没有人有什么想法?
最佳答案
It's as though Spring is not performing the necessary method conversion using _method before resolving the mapping.
这正是正在发生的事情。此转换由 HiddenHttpMethodFilter
执行,它是 disabled by default in Spring Boot 2.2 :
The filter that handles the
_method
request parameter is now disabled by default as it causes early consumption of a request body if the body may contain parameters. This can be restored by setting eitherspring.webflux.hiddenmethod.filter.enabled
orspring.mvc.hiddenmethod.filter.enabled
to true.
您正在使用 Spring MVC,因此您应该设置 spring.mvc.hiddenmethod.filter.enabled=true
。
关于spring-boot - Spring Boot 2.2.0.RELEASE 不映射 thymeleaf 放置和删除请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58564311/