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 ...
随机推荐
- Java 线程安全 与 锁
Java 线程安全 与 锁 多线程内存模型 线程私有栈内存 每个线程 私有的内存区域 进程公有堆内存 同一个进程 共有的内存区域 为什么会有线程安全问题? 多个线程同时具有对同一资源的操作权限,又发生 ...
- 深度优先搜索 DFS 学习笔记
深度优先搜索 学习笔记 引入 深度优先搜索 DFS 是图论中最基础,最重要的算法之一.DFS 是一种盲目搜寻法,也就是在每个点 \(u\) 上,任选一条边 DFS,直到回溯到 \(u\) 时才选择别的 ...
- 【生产事故调查】优化出来的bug-合并集合重复项
本来是要修复前一个代码bug,修复的过程中发现原本的代码又丑又长,复用性差(但是能用),出于强迫症忍不住的去优化,测试还不充分,火急火燎的发到生产了,结果掉井了!导致多个订单线下物流发货发多了.... ...
- 开发一款让我们慢慢变好的微信小程序
1. 前言 朋友,你还记得你想学编程最初的目的是什么吗? 先说说我的吧,我最初想学编程的目的只有一点,感觉编程很酷,会写代码的人很厉害!.随着后面参加工作,我马上产生了让我能够在编程这条路上继续走下去 ...
- 从零开始搭建高可用的k8s集群
一.环境准备 使用Hyper-V虚拟机功能搭建三台Centos虚拟机系统,配置好静态IP,分别为k8s-node1(192.168.0.8),k8s-node2(192.168.0.9),k8s-no ...
- Linux-centos8实现私有CA和证书申请
创建CA相关目录,centos8不存在这些目录,需手动建立 [root@centos8-liyj ~]#mkdir -pv /etc/pki/CA/{certs,cr1,newcerts,privat ...
- Linux-3作业练习
1.自建yum仓库,分别为网络源和本地源 请移步: yum源配置 2.编译安装http2.4,实现可以正常访问,并将编译步骤和结果提交. 请移步:http2.4编译安装 总结参照https ...
- .NET混合开发解决方案6 检测是否已安装合适的WebView2运行时
系列目录 [已更新最新开发文章,点击查看详细] 长青版WebView2运行时将作为Windows 11操作系统的一部分包含在内.但是在Windows 11之前(Win10.Win8.1.Win ...
- Java-GUI 编程之 JList、JComboBox实现列表框
目录 JList.JComboBox实现列表框 简单列表框 不强制存储列表项的ListModel和ComboBoxModel 强制存储列表项的DefaultListModel和DefaultCombo ...
- 机构:DARPA
DARPA,美国国防部高级研究计划局. 2021年3月19日,英特尔(Intel)宣布与美国国防部高级研究计划局(DARPA)达成的一项新合作,旨在推动在美制造的专用集成电路(ASIC)芯片的开发. ...