HTTP Status 405 - Request method 'GET' not supported?(尚硅谷Restful案例练习关于Delete方法出现的错误)
哈罗大家好,最近在如火如荼的学习java开发----Spring系列框架,当学习到SpringMVC,动手实践RESTFUL案例时,发现了以上报错405,get请求方法没有被支持。
- 首先第一步,我查看自己写的示例代码有无写错。在反复对比了尚硅谷发出来的示例代码后,发现并无错误;
- 然后我就根据错误在百度中畅游了不知多少春夏秋冬,然后并没有用,且部分解决办法并不适用我的问题情况。
- 由于浏览器只支持get和post,即使在form表单中设置method为put或delete,最后它们还是被当成get处理。
为了发送put请求和delete请求,Spring提供HiddenHttpMethodFilter。 如何使用HiddenHttpMethodFilter来发送put和delete请求?需要做到以下两点:
- 当前的请求方式必须为post。
- 当前请求必须传输参数_mothod,参数值为put或delete。

(以上第四点为什么必须是这样可以看源码,源码部分如下:)
1 public class HiddenHttpMethodFilter extends OncePerRequestFilter {
2 private static final List<String> ALLOWED_METHODS;
3 public static final String DEFAULT_METHOD_PARAM = "_method";
4 private String methodParam = "_method";
5
6 public HiddenHttpMethodFilter() {
7 }
8
9 public void setMethodParam(String methodParam) {
10 Assert.hasText(methodParam, "'methodParam' must not be empty");
11 this.methodParam = methodParam;
12 }
13
14 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
15 HttpServletRequest requestToUse = request;
16 if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {
17 String paramValue = request.getParameter(this.methodParam);
18 if (StringUtils.hasLength(paramValue)) {
19 String method = paramValue.toUpperCase(Locale.ENGLISH);
20 if (ALLOWED_METHODS.contains(method)) {
21 requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);
22 }
23 }
24 }
25
26 filterChain.doFilter((ServletRequest)requestToUse, response);
27 }
28
29 static {
30 ALLOWED_METHODS = Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name()));
31 }
32
33 private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
34 private final String method;
35
36 public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
37 super(request);
38 this.method = method;
39 }
40
41 public String getMethod() {
42 return this.method;
43 }
44 }
45 }
5.修改如图位置即可:

上面是尚硅谷的写法,我修改后的写法如下:

按照如上修改过后,不再报错,delete删除功能也能正常使用了。
文章如有不足之处,请留言评论或私信,希望我踩过的坑,满头泥泞,能帮助你们少踩坑,帮助到你java的学习!
HTTP Status 405 - Request method 'GET' not supported?(尚硅谷Restful案例练习关于Delete方法出现的错误)的更多相关文章
- There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported
背景:点击提交按钮ajax请求接口时,报出错误[ Whitelabel Error Page This application has no explicit mapping for /error, ...
- 使用SpringMVC时报错HTTP Status 405 - Request method 'GET' not supported
GET方法不支持.我出错的原因在于,在JSP中我希望超链接a以post方式提交,但是这里写js代码时出错. <script type="text/javascript"> ...
- springMVC出现HTTP Status 405 - Request method 'GET' not supported错误的解决方法
今天在写一个简单的springMVC的表单请求处理时,出现了这个问题.我的form表单用的是post方法提交,并没有使用get方法,出现这个问题时,笔者可谓是一脸懵逼. 这是form表单: 这是对po ...
- Restful风格,PUT修改功能请求,表单中存在文件报错-HTTP Status 405 - Request method 'POST' not supported
解决方案配置如下 <!-- 配置文件上传解析器 --> <bean id="multipartResolver" class="org.springfr ...
- SpringMVC框架出现 405 request method post not supported 的解决方法
在SpringMVC框架中当使用post请求服务,然后请求成功转到一个静态文件,如html,htm等网页时.页面出现405 request method post not supported错误,只要 ...
- Spring MVC出现POST 400 Bad Request &405 Request method 'GET' not supported
首先描述一下出现错误的情景: 我刚学springmvc,想做一个登录界面的东西.然后试着写了一个controller如下: @RequestMapping(value = "/login&q ...
- tomcat报错HTTP Status 405 - HTTP method GET is not supported by this URL
servlet提交表单,结果出错. 出现HTTP Status 405 - HTTP method GET is not supported by this URL 原因是:1.继承自Httpserv ...
- HTTP Status 405 - HTTP method GET is not supported by this URL
问题概述: 借助MyEclipse直接建立了一个Servlet类,每次访问这个Servlet都能访问.可自己建立一个Servlet为什么总提示:HTTP Status 405 - HTTP metho ...
- Feign发送Get请求时,采用POJO对象传递参数的最终解决方案 Request method 'POST' not supported (附带其余好几个坑)
yml: feign: httpclient: enabled: true properties: #feign feign.httpclient.enabled=true <!-- https ...
随机推荐
- linux 手动挂载硬盘没有移到回收站解决方法
linux 手动挂载硬盘没有移到回收站解决方法 修改挂载硬盘的文件夹权限为当前用户即可 或者 修改读写权限 chmod 777 mount-disk-path
- 【转】python代码优化常见技巧
https://blog.csdn.net/egefcxzo3ha1x4/article/details/97844631
- WPF行为基础
理解行为 复杂的UI效果(缩放.拖拽.平滑等)通过样式与触发器比较难以实现,通过引入行为模型来实现.使用行为也可以处理UI操作之外的业务 程序集引用 System.Windows.Interactiv ...
- HCNP Routing&Switching之Super VLAN
前文我们了解了VLAN隔离技术MUX VLAN相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/16196936.html:今天我们来聊一聊VLAN优化S ...
- Ubuntu 系统安装,VMware
系统版本 ubuntu-18.04.5-server-amd64.iso 1.自定义安装 2.默认下一步 3. 稍后安装操作系统 4.选择ubuntu 64位 5.选额安装的目录 6.设置虚拟机c ...
- MongoDB 常用运维实践总结
关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 一.MongoDB 集群简介 MongoDB是一个基于分布式文件存储的数据库,其目的在于为WE ...
- 基于SqlSugar的开发框架循序渐进介绍(2)-- 基于中间表的查询处理
在前面介绍的SqlSugar的相关查询处理操作中,我们主要以单表的方式生成相关的实体类,并在查询的时候,对单表的字段进行条件的对比处理,从而返回对应的数据记录.本篇随笔介绍在一些外键或者中间表的处理中 ...
- docker-compose 启动 rabbitmq
说明 前提条件 ubuntu-20.04-server docker & docker-compose 安装参考 安装 准备 rabbitmq.conf 新建 rabbitmq.conf 文件 ...
- 陈胡:Apache SeaTunnel实现 非CDC数据抽取实践
导读: 随着全球数据量的不断增长,越来越多的业务需要支撑高并发.高可用.可扩展.以及海量的数据存储,在这种情况下,适应各种场景的数据存储技术也不断的产生和发展.与此同时,各种数据库之间的同步与转化的需 ...
- linux篇-linux修改网卡名(亲测有效)
1查看网卡ip addr 2cd /etc/sysconfig/network-scripts Ls查看 3mv ifcfg-eno16777736 ifcfg-eth0重命名,然后编辑 最后一行加入 ...