SpringBoot@DeleteMapping(/xxx/{id})请求报405的解决

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot@DeleteMapping(/xxx/{id})请求报405的解决

薛定谔的猿...   2023-02-03 我要评论

SpringBoot@DeleteMapping(/xxx/{id})请求报405

在学习SpringBoot2.x实现 restful 的delete操作时发现在表单中添加

<input type="hidden" name="_method" value="delete"/>

后台用@deleteMapping(/xxx/{id})这种方式会报405(不允许的访问方式)

各种百度,后来发现在 Spring Boot 的 META-INF/spring-configuration-metadata.json 配置文件中,默认是关闭Spring 的 hiddenmethod 过滤器的


然后我们需要在springBoot的配置文件中将它手动开启即可;

spring.mvc.hiddenmethod.filter.enabled=true

SpringBoot---@DeleteMapping

@DeleteMapping

在编写代码时引用了*@DeleteMapping*注解

@DeleteMapping(value = "/remove/{courseNo}" )
    public String remove(@PathVariable("courseNo") String courseNo){
        courseService.removeCourseByNo(courseNo);
        return "redirect:/course/list";

但运行程序页面无结果返回,出现POST错误。经百度查询,Spring系统配置hiddenmethod过滤器默认是False,解决结果在application.properties添加一行代码,启用过滤器:

spring.mvc.hiddenmethod.filter.enabled=true

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们