解决springboot项目在获得@DeleteMapping请求时出错,页面显示Request method 'POST' not supported的方法

最近在学习一个springboot项目的时候,在获得@DeleteMapping请求时出错,页面显示Request method ‘POST’ not supported

controller层的代码

@DeleteMapping("/xx/{id}")
public String deleteEmployee(@PathVariable("id")Integer id){

    user.delete(id);
    return "redirect:/index";
}

表单代码

<form th:action="@{/xx/}+${user.id}" method="post">
   <input type="hidden" name="_method" value="delete">
   <button  type="submit" >删除</button>
</form>

运行后出错
在这里插入图片描述
然后苦逼各种搜解决方法 都没成功,便翻看源码,如下:
在这里插入图片描述
matchIfMissing的用法:
(1)如果配置文件中没有配置 spring.mvc.hiddenmethod.filter.enabled 的属性,并且matchIfMissing = false,那么不符合加载

hiddenHttpMethodFilter()的条件,反之matchIfMissing = true,符合加载hiddenHttpMethodFilter()的条件。
(2)如果配置文件中配置了 spring.mvc.hiddenmethod.filter.enabled = false,不管matchIfMissing = false 或者 matchIfMissing = true,都不符合加载hiddenHttpMethodFilter()的条件。

(3)如果配置文件中配置了 spring.mvc.hiddenmethod.filter.enabled = true ,不管matchIfMissing = false 或者 matchIfMissing = true,都符合加载hiddenHttpMethodFilter()的条件。

所以得出解决办法:
在application.yml或者application.properties中加入spring.mvc.hiddenmethod.filter.enabled = true 即可

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值