2

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

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
Rejkid
  • 109
  • 1
  • 6
  • Because the web browser sends *all* form values of a form with `multipart/form-data` encoding as part of the multipart content, so you need to say you want the value from there, otherwise the server will try to extract the value from a query parameter, and since the browser didn't send any query parameters, you'd get all null values. – Andreas May 14 '19 at 03:42

1 Answers1

2

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.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720