Spring Boot 2.2.0.RELEASE未映射百里香放置和删除请求

时间:2019-10-25 19:02:59

标签: spring-boot thymeleaf

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

我在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)
...

It's as though Spring is not performing the necessary method conversion using _method before resolving the mapping.

Does anyone have any ideas?

1 个答案:

答案 0 :(得分:2)

  

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

这正是正在发生的事情。此转换由HiddenHttpMethodFilter执行,它是disabled by default in Spring Boot 2.2

  

处理_method请求参数的过滤器现在默认情况下处于禁用状态,因为如果请求正文可能包含参数,则会导致请求主体的早期消耗。可以通过将spring.webflux.hiddenmethod.filter.enabledspring.mvc.hiddenmethod.filter.enabled设置为true来恢复。

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