springboot中實現PUT、DELETE請求

1.首先springboot自動加載了兩個Filterhtml

2017-04-28 14:10:58.352  INFO 23781 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-04-28 14:10:58.355  INFO 23781 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2.而後須要在表單中增長一個隱藏域name爲「_method」並且MIME類型必須是multipart/form-data.在服務器端依然接受PUT請求就好。依然是以POST方式提交,而不是PUT或者DELETE.目前猜想是由於tomcat默認限制了除get post以外的請求,可是未測試。因此這是spring 本身的封裝。下面是源碼spring

public class HiddenHttpMethodFilter extends OncePerRequestFilter {

   /** Default method parameter: {@code _method} */
   public static final String DEFAULT_METHOD_PARAM = "_method";//這裏能夠看到表單域爲_method

   private String methodParam = DEFAULT_METHOD_PARAM;


   /**
    * Set the parameter name to look for HTTP methods.
    * @see #DEFAULT_METHOD_PARAM
    */
   public void setMethodParam(String methodParam) {
      Assert.hasText(methodParam, "'methodParam' must not be empty");
      this.methodParam = methodParam;
   }

   @Override
   protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
         throws ServletException, IOException {

      HttpServletRequest requestToUse = request;
        //這裏能夠看到這個filter只是過濾了POST請求。若是表單域中有_method的話就作了包裝處理
      if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
         String paramValue = request.getParameter(this.methodParam);
         if (StringUtils.hasLength(paramValue)) {
            requestToUse = new HttpMethodRequestWrapper(request, paramValue);
         }
      }

      filterChain.doFilter(requestToUse, response);
   }


   /**
    * Simple {@link HttpServletRequest} wrapper that returns the supplied method for
    * {@link HttpServletRequest#getMethod()}.
    */
   private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {

      private final String method;

      public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
         super(request);
         this.method = method.toUpperCase(Locale.ENGLISH);
      }

      @Override
      public String getMethod() {
         return this.method;
      }
   }

}

3.tomcat

 HttpPutFormContentFilter

由HiddenHttpMethodFilter可知,html中的form的method值只能爲post或get,咱們能夠經過HiddenHttpMethodFilter獲取put表單中的參數鍵值對,而在Spring3中獲取put表單的參數鍵值對還有另外一種方法,即便用HttpPutFormContentFilter過濾器。springboot

HttpPutFormContentFilter過濾器的做爲就是獲取put表單的值,並將之傳遞到Controller中標註了method爲RequestMethod.put的方法中。服務器

與HiddenHttpMethodFilter不一樣,在form中不用添加參數名爲_method的隱藏域,且method沒必要是post,直接寫成put,但該過濾器只能接受enctype值爲application/x-www-form-urlencoded的表單,也就是說,在使用該過濾器時,form表單的代碼必須以下:app

<form action="" method="put" enctype="application/x-www-form-urlencoded">  ide

......  post

</form> 測試

可是通過個人測試這種方法是行不通的,有多是我使用的spring版本不對,可是我想在springboot啓動的時候就加載了這個filter確定是有他的用處的。並且看源碼也確實是沒有問題的。等有時間再多測試下吧。this

參考原文:http://blog.csdn.net/qyp1314/article/details/42023725

相關文章
相關標籤/搜索