@FormDataParam 和 @FormParam 有什么区别

@FormDataParam@FormParam有什么区别?

我在一个方法中使用了多个 @FormDataParam 但它引发了媒体不支持的类型错误.但是当我使用 @FormParam 时,我得到了值.

I was using multiple @FormDataParam in a method but it was throwing media unsupported type error. But when I used @FormParam, I got the values.

那么,我需要知道它们两者之间有什么区别?

So, I need to know what is the difference between the two of them?

推荐答案

  • @FormDataParam 应该与 Multipart 类型数据一起使用(即 multipart/form-dataMediaType.MULTIPART_FORM_DATA),其原始形式看起来像

    • @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 主要用于发送二进制数据,如非文本文件,或随文件发送任意、元数据或相关数据.

      Multipart is mainly used for sending binary data, like non-text files, or sending arbitrary, meta, or related data along with files.

      @FormParam 用于 url 编码的请求参数(即 application/x-www-form-urlencodedMediaType.APPLICATION_FORM_URLENCODED),原始形式看起来像

      @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&param2=value2
      

    • 这两种类型主要用于客户端表单.例如

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

      <form method="POST" action="someUrl">
          <input name="gender" type="text">
          <input name="name" type="text">
      </form>
      

      上面会以 application/x-www-form-urlencoded 的形式发送请求参数.它将以原始形式发送为

      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
      

      在服务器端,我们可以为表单中的每个命名参数使用一个@FormParam

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

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

      但是如果我们需要发送图像和参数,application/x-form-url-encoded 数据类型是不够的,因为它只处理文本.所以我们需要使用Multipart

      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

      <form method="POST" action="someUrl", enctype="multipart/form-data">
          <input name="gender" type="text">
          <input name="name" type="text">
          <input name="avatar" type="file">
      </form>
      

      这里指定了 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--
      

      在服务器端,与上面的 application/x-www-form-urlencoded 示例类似,对于每个 Multipart 参数(或更准确的字段),我们可以使用 @FormDataParam 表示每个参数

      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
      

      另请参阅:

      • HTML 文档中的表单

相关文章