为什么要"FormDataMultiPart"?类型参数被不同地对待 [英] Why "FormDataMultiPart" type parameter is treated differently

查看:162
本文介绍了为什么要"FormDataMultiPart"?类型参数被不同地对待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型Web应用程序,将文件上传到Web服务器.我可以正常工作,但是我感到困惑的是,几乎所有从客户端(浏览器)发送的参数都必须在服务器端注入 @FormDataParam 字样,而 FormDataMultiPart >类型参数.有人可以向我解释一下吗?

关于,贾努斯(Janusz)

解决方案

通常,所有实体主体参数都是没有任何注释的参数.例如JSON或XML,您会看到

  @POST@Consumes({"application/json","application/xml"})公共响应帖子(RequestEntity实体){} 

在这里,用于处理JSON或XML的 MessageBodyReader 将用于处理将实体流反序列化为 RequestEntity .

FormDataMultiPart 相同.有一个 MessageBodyReader 可以处理反序列化整个多部分流,并创建 FormDataBodyPart 以便Jersey运行时在调用资源方法时传递给资源方法.

@FormDataParam 更像是 @FormParam @PathParam @QueryParam 等.我们可以有多个参数.此注释的每个参数代表多部分请求的单个 part .

因此,我们可以选择使用 FormDataMultiPart 将整个请求放入单个实体中;在这种情况下,我们将以编程方式提取零件.或者我们可以使用 @FormDataParam 批注以声明方式提取每个部分,其中批注的值将是该部分的 name .

如果我们使用 FormDataMultiPart ,则必须从中手动提取所有零件.在每个部分的名称未知的情况下,我们将使用它.或者有很多部分,其中每个部分对于开发人员来说都是繁琐的工作或丑陋的.

I am writing little web app uploading the file to a web server. I got everything working, but I became puzzled by the fact that almost all parameters send from the client (browser) must be on the server side injected with the word @FormDataParam except FormDataMultiPart type parameter. Can somebody explain that to me, please?

Regards, Janusz

解决方案

Generally, all entity body parameters are the parameter without any annotation. Such as with JSON or XML, you would see

@POST
@Consumes({"application/json", "application/xml"})
public Response post(RequestEntity entity) {
}

Here, the MessageBodyReader that handles JSON or XML would be used to handle deserializing the entity stream into the RequestEntity.

This is the same with FormDataMultiPart. There is a MessageBodyReader to handle deserializing the entire multipart stream and creating the FormDataBodyPart for the Jersey runtime to pass to the resource method when it is called.

@FormDataParam is treated more like @FormParam, @PathParam, @QueryParam, etc. We can have multiple parameters. Each parameter for this annotation represent a single part of the multipart request.

So we have the option to either get the entire request into a single entity with FormDataMultiPart; in which case we would extract the parts programmatically. Or we can declaratively extract each part using the @FormDataParam annotation, where the value of the annotation would be the name of the part.

If we used FormDataMultiPart, we would have to extract all the parts manually from it. We would use this in such a case where the name of each part is not known. Or there are many parts, where declaring each one is tedious work or ugly to the developer.

这篇关于为什么要"FormDataMultiPart"?类型参数被不同地对待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆