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 ...
随机推荐
- [Vue]写一个简单的文件上传控件
这篇将介绍如何写一个简单的基于Vue+Element的文件上传控件. 控件将具有 1. 上传队列的列表,显示文件名称,大小等信息,可以显示上传进度实时刷新 2. 取消上传 使用Element的u ...
- 《手把手教你》系列基础篇(九十五)-java+ selenium自动化测试-框架之设计篇-java实现自定义日志输出(详解教程)
1.简介 前面宏哥一连几篇介绍如何通过开源jar包Log4j.jar.log4j2.jar和logback实现日志文件输出,Log4j和logback确实很强大,能生成三种日志文件,一种是保存到磁盘的 ...
- 一个比 Nginx 功能更强大的 Web 服务器
公众号关注 「开源Linux」 回复「学习」,有我为您特别筛选的学习资料~ Caddy 简介 Caddy 是一个 Go 编写的 Web 服务器,类似于 Nginx,Caddy 提供了更加强大的功能,随 ...
- SSH只能用于远程Linux主机?那说明你见识太小了!
开源Linux 长按二维码加关注~ 今天小编为大家分享一篇关于SSH 的介绍和使用方法的文章.本文从SSH是什么出发,讲述了SSH的基本用法,之后在远程登录.端口转发等多种场景下进行独立的讲述,希望能 ...
- go 语言开发2 简易数据库和web代码示例
数据库开发示例 package dao import ( "github.com/go-xorm/xorm" "fmt" ) type UserInfo str ...
- EF Core 配置模型
0 前言 本文的第一节,会概述配置模型的作用(对数据模型的补充描述). 第二节描述两种配置方式,即:数据注释(data annotations)和 Fluent API 方式. 第三节开始,主要是将常 ...
- ubuntu helpers
linux 命令大全 Apt proxy configuration on Ubuntu 20.04 Focal Fossa Linux 临时使用socks代理apt-get的方法 docker - ...
- linux篇-linux mysql数据库定时备份
1在linux上面创建一个文件夹,并且进行备份 cd /home mkdir backup cd backup 2创建一个脚本 Vi imaginebase.sh #!/bin/bash mysqld ...
- java框架--快速入门
spring快速入门 1.创建项目 1.1创建项目文件夹 1.2启动idea ->文件->打开->点击创建的项目文件夹 1.3右键创建 ...
- 软件性能测试分析与调优实践之路-JMeter对RPC服务的性能压测分析与调优-手稿节选
一.JMeter 如何通过自定义Sample来压测RPC服务 RPC(Remote Procedure Call)俗称远程过程调用,是常用的一种高效的服务调用方式,也是性能压测时经常遇到的一种服务调用 ...