(一)问题描述
在进行springboot的Restful风格的CRUD的学习时,需要实现restfull风格的删除操作,以delete方式提交,最后发现依然是以post方式提交。因此出现了“Request method 'POST' not supported”问题。
(二)所写的代码
我的controller代码是:
//员工删除
@DeleteMapping("/emp/{id}")
public String deleteEmployee(@PathVariable("id") Integer id){
System.out.println("id就是。。。。。。。。。。。"+id);
employeeDao.delete(id);
return "redirect:/emps";
}
html的代码
<form th:action="@{/emp/}+${emp.id}" method="post">
<input type="hidden" name="_method" value="delete"/>
<button type="submit" class="btn btn-sm btn-danger">删除</button>
</form>
(三)错误信息
在点击 删除按钮时,出现了以下错误页面:
(四)解决方案:
在application.properties中添加一代码,可以解决。
#可以把 POST 请求转为 DELETE 或 POST 请求
spring.mvc.hiddenmethod.filter.enabled=true
(五)问题解析:
原因是在 SpringBoot 的 META-INF/spring-configuration-metadata.json 配置文件中,
默认是关闭 Spring 的 hiddenmethod 过滤器的。( springboot在2.0以上默认关闭这个方法了)