哈罗大家好,最近在如火如荼的学习java开发----Spring系列框架,当学习到SpringMVC,动手实践RESTFUL案例时,发现了以上报错405,get请求方法没有被支持。

  1. 首先第一步,我查看自己写的示例代码有无写错。在反复对比了尚硅谷发出来的示例代码后,发现并无错误;
  2. 然后我就根据错误在百度中畅游了不知多少春夏秋冬,然后并没有用,且部分解决办法并不适用我的问题情况。
  3. 由于浏览器只支持get和post,即使在form表单中设置method为put或delete,最后它们还是被当成get处理。
    为了发送put请求和delete请求,Spring提供HiddenHttpMethodFilter。
  4. 如何使用HiddenHttpMethodFilter来发送put和delete请求?需要做到以下两点:

    1. 当前的请求方式必须为post。
    2. 当前请求必须传输参数_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方法出现的错误)的更多相关文章

  1. 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, ...

  2. 使用SpringMVC时报错HTTP Status 405 - Request method 'GET' not supported

    GET方法不支持.我出错的原因在于,在JSP中我希望超链接a以post方式提交,但是这里写js代码时出错. <script type="text/javascript"> ...

  3. springMVC出现HTTP Status 405 - Request method 'GET' not supported错误的解决方法

    今天在写一个简单的springMVC的表单请求处理时,出现了这个问题.我的form表单用的是post方法提交,并没有使用get方法,出现这个问题时,笔者可谓是一脸懵逼. 这是form表单: 这是对po ...

  4. Restful风格,PUT修改功能请求,表单中存在文件报错-HTTP Status 405 - Request method 'POST' not supported

    解决方案配置如下 <!-- 配置文件上传解析器 --> <bean id="multipartResolver" class="org.springfr ...

  5. SpringMVC框架出现 405 request method post not supported 的解决方法

    在SpringMVC框架中当使用post请求服务,然后请求成功转到一个静态文件,如html,htm等网页时.页面出现405 request method post not supported错误,只要 ...

  6. Spring MVC出现POST 400 Bad Request &405 Request method 'GET' not supported

    首先描述一下出现错误的情景: 我刚学springmvc,想做一个登录界面的东西.然后试着写了一个controller如下: @RequestMapping(value = "/login&q ...

  7. 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 ...

  8. HTTP Status 405 - HTTP method GET is not supported by this URL

    问题概述: 借助MyEclipse直接建立了一个Servlet类,每次访问这个Servlet都能访问.可自己建立一个Servlet为什么总提示:HTTP Status 405 - HTTP metho ...

  9. Feign发送Get请求时,采用POJO对象传递参数的最终解决方案 Request method 'POST' not supported (附带其余好几个坑)

    yml: feign: httpclient: enabled: true properties: #feign feign.httpclient.enabled=true <!-- https ...

随机推荐

  1. KotlinMall实战之注册部分MVP架构配置

    包目录如下: ①BaseView部分:基本的回调 interface BaseView { fun showLoading() fun hideLoading() fun onError()} ②Ba ...

  2. python基础练习题(题目 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少)

    day13 --------------------------------------------------------------- 实例021:猴子偷桃 题目 猴子吃桃问题:猴子第一天摘下若干 ...

  3. 比 Navicat 还要好用、功能更强大的工具!

    DBeaver 是一个基于 Java 开发,免费开源的通用数据库管理和开发工具,使用非常友好的 ASL 协议.可以通过官方网站或者 Github 进行下载. 由于 DBeaver 基于 Java 开发 ...

  4. bat脚本删除一周前的文件

    bat脚本删除7天前的文件 @echo off forfiles /p D:\logstash-1.4.2\bin\ /m *.log -d -7 /C "cmd /c del /f @pa ...

  5. 深度长文:深入理解Ceph存储架构

    点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 本文是一篇Ceph存储架构技术文章,内容深入到每个存储特 ...

  6. 8 种常见 SQL 错误用法

    点击上方"开源Linux",选择"设为星标"回复"学习"获取独家整理的学习资料! 1.LIMIT 语句 分页查询是最常用的场景之一,但也通常 ...

  7. QT快速入门

    QT快速入门 本文档将介绍QT工程的创建.UI界面布局,并以计数器为例了解QT中多线程的用法,最终完成一个基础的QT项目. 1 创建QT工程文件 在安装好QT之后,能够在其安装组件中找到Qt Crea ...

  8. animation—延迟和持续时间

    animation: moveToRight .75s 6s linear  infinite ;animation: moveToRight -.75s 1.5s  linear infinite; ...

  9. position与float

    position:fixed/absolute和float的关系:元素设置position:absolute / fixed后,float属性是没有效果的.对于position: absolute元素 ...

  10. 手脱UPX(3.91)

    1.使用Detect It Easy进行查壳: 2.使用x32dbg打开该带壳程序,在选项->选项->异常中添加区间设置0~FFFFFFFF全忽略: 3.我们F9运行到程序入口处,看到了p ...