springboot项目在获得@DeleteMapping请求时出错,页面显示Request method 'POST' not supported
最近在学习一个springboot项目的时候,在获得@DeleteMapping请求时出错,页面显示Request method 'POST' not supported
报错页面显示我已经获得到了我需要请求的值加上请求的参数
controller层的代码
@DeleteMapping("/happy/{id}") public String deleteEmployee(@PathVariable("id")Integer id){ user.delete(id); return "redirect:/index"; }
用REST风格的表单
<form th:action="@{/happy/}+${user.id}" method="post"> <input type="hidden" name="_method" value="delete"> <button type="submit" >删除</button> </form>
运行后莫名其妙的报错
项目中也有同样的form表单发送的是put请求,也可以正常运行
<input type="hidden" name="_method" value="put">
在controller层用@PutMapping的方法也可以获取请求,成功响应后返回指定的页面
只有delete的请求获取不到,显示如上的报错信息
搜寻了一早上,网友指示的方法基本都试过了无果。
于是索性不用REST的方法,中规中矩的写请求参数和Mapping方法
删除了form表单,换成a标签
<a class="btn" th:href="@{/happy/}+${user.id}">删除</a>
controller层改成传统的请求方式
@RequestMapping("/happy/{id}")
项目是不报错了,问题还没解决,为什么put请求就不会报错,delete就会出错
还有如果我可以使用传统的获取请求方式@RequestMapping,为什么非得要用REST风格呢,他的优点仅在于方便辨识吗
带着求知的渴望,期待能和各位大神交流,吸取经验
附上一位大牛总结的这个问题,很全面,希望能帮助到有需要的人