最近学习了RESTful服务的构建,在试验@DeleteMapping 删除功能的时候,出现了很奇怪的错误。
点击删除按钮,就会出现如下错误信息:
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported.
查了一天文档,没有结果,后来发现教程中Springboot 1.5.10中的WebMvcConfigurerAdapter 方法在2.1X版本中已经不推荐使用了,于是使用了 WebMvcConfigurationSupport 替代。
WebMvcConfigurationSupport 指示SpringBoot 要完全接管SpringMVC的配置,这意味着HiddenHttpMethodFilter 不再自动配置。因此,DELETE 请求被作为POST请求处理。
正确的用法是自己配置 HiddenHttpMethodFilter bean或者使用SpringBoot的自动配置,推荐使用后者。将MyMvcConfig 修改为实现WebMvcConfigurer ,问题解决。
修改后的代码如下:
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addI