HttpRequestMethodNotSupportedException: Request method ‘POST‘ not supported解决方法

(一)问题描述

在进行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以上默认关闭这个方法了)

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值