HTTP Status 405 - Request method 'GET' not supported?(尚硅谷 ...

打印 上一主题 下一主题

主题 878|帖子 878|积分 2634

哈罗大家好,最近在如火如荼的学习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. 1 public class HiddenHttpMethodFilter extends OncePerRequestFilter {
  2. 2     private static final List<String> ALLOWED_METHODS;
  3. 3     public static final String DEFAULT_METHOD_PARAM = "_method";
  4. 4     private String methodParam = "_method";
  5. 5
  6. 6     public HiddenHttpMethodFilter() {
  7. 7     }
  8. 8
  9. 9     public void setMethodParam(String methodParam) {
  10. 10         Assert.hasText(methodParam, "'methodParam' must not be empty");
  11. 11         this.methodParam = methodParam;
  12. 12     }
  13. 13
  14. 14     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  15. 15         HttpServletRequest requestToUse = request;
  16. 16         if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {
  17. 17             String paramValue = request.getParameter(this.methodParam);
  18. 18             if (StringUtils.hasLength(paramValue)) {
  19. 19                 String method = paramValue.toUpperCase(Locale.ENGLISH);
  20. 20                 if (ALLOWED_METHODS.contains(method)) {
  21. 21                     requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);
  22. 22                 }
  23. 23             }
  24. 24         }
  25. 25
  26. 26         filterChain.doFilter((ServletRequest)requestToUse, response);
  27. 27     }
  28. 28
  29. 29     static {
  30. 30         ALLOWED_METHODS = Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name()));
  31. 31     }
  32. 32
  33. 33     private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
  34. 34         private final String method;
  35. 35
  36. 36         public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
  37. 37             super(request);
  38. 38             this.method = method;
  39. 39         }
  40. 40
  41. 41         public String getMethod() {
  42. 42             return this.method;
  43. 43         }
  44. 44     }
  45. 45 }
复制代码
  5.修改如图位置即可:
  
 
  上面是尚硅谷的写法,我修改后的写法如下:
  
 
  按照如上修改过后,不再报错,delete删除功能也能正常使用了。
  文章如有不足之处,请留言评论或私信,希望我踩过的坑,满头泥泞,能帮助你们少踩坑,帮助到你java的学习!
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美丽的神话

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表