What is difference between @FormDataParam and @FormParam

前端 未结 2 1759
情书的邮戳
情书的邮戳 2020-12-14 03:12

What is the difference between @FormDataParam and @FormParam?

I was using multiple @FormDataParam in a method but it was throw

2条回答
  •  爱一瞬间的悲伤
    2020-12-14 03:30

    • @FormDataParam is supposed to be used with Multipart type data (i.e. multipart/form-data or MediaType.MULTIPART_FORM_DATA), which in its raw form looks something like

      Content-Type: multipart/form-data; boundary=AaB03x
      
      --AaB03x
      Content-Disposition: form-data; name="submit-name"
      
      Larry
      --AaB03x
      Content-Disposition: form-data; name="files"; filename="file1.txt"
      Content-Type: text/plain
      
      ... contents of file1.txt ...
      --AaB03x--
      

      Multipart is mainly used for sending binary data, like non-text files.

    • @FormParam is for url-encoded request parameters (i.e. application/x-www-form-urlencoded or MediaType.APPLICATION_FORM_URLENCODED), which in raw form looks like

      param1=value1¶m2=value2
      

    Both of these types are mainly used in client side forms. For example

    the above would send the request parameters as application/x-www-form-urlencoded. It would get sent in raw form as

    gender=male&name=peeskillet
    

    On the server side, we can use a @FormParam for each named parameter in the form

    @FormParam("gender") String gender, @FormParam("name") String name
    

    But if we need to send say an image along with the parameters, application/x-form-url-encoded data type is not sufficient, as it only deals with text. So we need to use Multipart

    Here the Multipart type is specified, now the browser will send out the request with something like

    Content-Type: multipart/form-data; boundary=AaB03x
    
    --AaB03x
    Content-Disposition: form-data; name="gender"
    
    Male
    --AaB03x
    Content-Disposition: form-data; name="name"
    
    Peskillet
    --AaB03x
    Content-Disposition: form-data; name="avatar"; filename="image.png"
    Content-Type: image/png
    
    ... binary content of image file ...
    --AaB03x--
    

    On the server, similar with the application/x-www-form-urlencoded example above, for each Multipart parameter (or field to be more precise), we can use @FormDataParam to signify each parameter

    @FormDataParam("gender") String gender,
    @FormDataParam("name") String name,
    @FormDataParam("avatar") InputStream avatar
    

    See Also:

    • Forms in HTML Documents

提交回复
热议问题