0

Consider following function:

@POST
    @Path("/handle_response")
    @Produces(MediaType.APPLICATION_JSON)
    public ResponseJsonBean handle(@FormParam("first") Integer first, @FormParam("second") Integer second) 
    {
        ////// bla bla
    }

The above function is called when I make a POST x-www-form-urlencoded request. But the function is not called when I make POST form data request. Why it is not called in the latter case? and how can I make such function which works for latter request.

Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57

1 Answers1

2

Yeah application/x-www-form-urlencoded and multipart/form-data are complete different types and formats. The correct provider to read the request is discovered through the type sent.

For multipart support, you should add the Jersey multipart dependency

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>${jersey2.version}</version>
</dependency>

Then in your resource method do something like

@POST
@Path("/upload")
@Produces(MediaType.APPLICATION_JSON)
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadFile(
        @FormDataParam("file") InputStream fileInputStream,
        @FormDataParam("file") FormDataContentDisposition cdh) throws Exception{
}

See Also:

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Are multipart/form-data and form-data alone, same things? – Aqeel Ashiq Jul 27 '15 at 17:44
  • There's no such thing (or at least media type) as form-data – Paul Samsotha Jul 27 '15 at 17:46
  • Actually I have to integrate my rest web service with a developer who is POSTing simple parameters using form-data. There is no file to upload. – Aqeel Ashiq Jul 27 '15 at 17:46
  • Why did you tag your question `multipartform-data` then. There is no such media type as `form-data` why don't you figure out the exact media type you need, then maybe I can answer your question better – Paul Samsotha Jul 27 '15 at 17:47
  • Also multipart/form-data doesn't need to be file. It can be simple text if you want. But I don't see why you would want to use multipart for simple text, when form-urlencoded is just fine. See [here](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4) to see the difference between the two and when they should be used – Paul Samsotha Jul 27 '15 at 17:51
  • please also tell how to extract string parameters from multiparr form-data – Aqeel Ashiq Jul 27 '15 at 18:40
  • Did you try to simply put a String as a parameter? – Paul Samsotha Jul 27 '15 at 18:41