Java @FormDataParam 和 @FormParam 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37537611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is difference between @FormDataParam and @FormParam
提问by Partha Sarathi Ghosh
What is the difference between @FormDataParam
and @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.
我@FormDataParam
在一个方法中使用了 multiple但它抛出了媒体不支持的类型错误。但是当我使用时 @FormParam
,我得到了值。
So, I need to know what is the difference between the two of them?
所以,我需要知道他们两个有什么区别?
采纳答案by Paul Samsotha
@FormDataParam
is supposed to be used with Multipart type data (i.e.multipart/form-data
orMediaType.MULTIPART_FORM_DATA
), which in its raw form looks something likeContent-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
orMediaType.APPLICATION_FORM_URLENCODED
), which in raw form looks likeparam1=value1¶m2=value2
@FormDataParam
应该与 Multipart 类型数据(即multipart/form-data
或MediaType.MULTIPART_FORM_DATA
)一起使用,其原始形式看起来像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 主要用于发送二进制数据,如非文本文件。
@FormParam
用于 url 编码的请求参数(即application/x-www-form-urlencoded
或MediaType.APPLICATION_FORM_URLENCODED
),原始形式看起来像param1=value1¶m2=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
以上将请求参数发送为application/x-www-form-urlencoded
. 它将以原始形式发送
gender=male&name=peeskillet
On the server side, we can use a @FormParam
for each named parameter in the form
在服务器端,我们可以@FormParam
对表单中的每个命名参数使用 a
@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
但是如果我们需要发送一个图像和参数,application/x-form-url-encoded
数据类型是不够的,因为它只处理文本。所以我们需要使用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
这里指定了 Multipart 类型,现在浏览器将发出类似这样的请求
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
在服务器上,与application/x-www-form-urlencoded
上面的例子类似,对于每个 Multipart 参数(或者更精确的字段),我们可以使用@FormDataParam
来表示每个参数
@FormDataParam("gender") String gender,
@FormDataParam("name") String name,
@FormDataParam("avatar") InputStream avatar
See Also:
也可以看看:
回答by Rahul Tripathi
From the documentation FormParam:
从文档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.
将包含在请求实体正文中的表单参数的值绑定到资源方法参数。值是 URL 解码的,除非使用 Encoded 注释禁用它。可以使用 DefaultValue 注释指定默认值。如果请求实体主体不存在或者是不受支持的媒体类型,则使用默认值。
and FormDataParam
Binds the named body part(s) of a "multipart/form-data" request entity body to a resource method parameter.
将“multipart/form-data”请求实体主体的命名主体部分绑定到资源方法参数。