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 ...
随机推荐
- 如何在代码层面提供CPU分支预测效率
关于分支预测的基本概念和详细算法可以参考我之前写的知乎回答,基本概念不再阐述了~~ https://www.zhihu.com/question/486239354/answer/2410692045 ...
- TCP 连接的建立 & 断开
TCP 连接的建立过程 一开始,客户端和服务端都处于 close 状态. 先是服务端监听某个端口,此时服务端处于 listen 状态. 这个时候客户端就可以发送连接请求报文了. 第一次握手 客户端会主 ...
- Spring 源码(7)Spring的注解是如何解析的?
上一篇 https://www.cnblogs.com/redwinter/p/16196359.html 介绍了BeanFactoryPostProcessor的执行过程,这篇文章介绍Spring中 ...
- NLP教程(3) | 神经网络与反向传播
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/36 本文地址:http://www.showmeai.tech/article-det ...
- 一文说透 MySQL JSON 数据类型(收藏)
JSON 数据类型是 MySQL 5.7.8 开始支持的.在此之前,只能通过字符类型(CHAR,VARCHAR 或 TEXT )来保存 JSON 文档. 相对字符类型,原生的 JSON 类型具有以下优 ...
- css自定义省略实例1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ruby 趣学笔记(二)
Ruby 趣学笔记(二) 本文写于 2020 年 5 月 7 日 类的继承 之前忘记写了,Ruby 的继承写法是: class IPhone < Phone def initialize(id, ...
- html5 tts(文字朗读)
在 chrome 下使用比较好的中文语音包. 注意 speechSynthesis.getVoices() 有时候可能会返回空数组,需要做验证 var zhCnLangs = speechSynthe ...
- Mac-Typora快捷键
标题(大钢) command+(1-6)) 如:command+1,设置为一级标题 引用 快捷键:command+option+Q 或者:先">",后面直接加内容 二级引用: ...
- netty系列之:protobuf在UDP协议中的使用
目录 简介 UDP在netty中的表示 DatagramPacketEncoder DatagramPacketDecoder 总结 简介 netty中提供的protobuf编码解码器可以让我们直接在 ...