Why “FormDataMultiPart” type parameter is treated differently

为君一笑 提交于 2019-12-24 00:28:00

问题


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


回答1:


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 not pleasing to the coder.



来源:https://stackoverflow.com/questions/56122459/why-formdatamultipart-type-parameter-is-treated-differently

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