What is difference between @FormDataParam and @FormParam

被刻印的时光 ゝ 提交于 2019-12-17 22:55:05

问题


What is the difference between @FormDataParam and @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?


回答1:


  • @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&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>

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

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

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



回答2:


From the documentation FormParam:

Binds the value(s) of a form parameter contained within a request entity body to a resource method parameter. Values are URL decoded unless this is disabled using the Encoded annotation. A default value can be specified using the DefaultValue annotation. If the request entity body is absent or is an unsupported media type, the default value is used.

and FormDataParam

Binds the named body part(s) of a "multipart/form-data" request entity body to a resource method parameter.



来源:https://stackoverflow.com/questions/37537611/what-is-difference-between-formdataparam-and-formparam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!