2

I am creating a mobile API that accepts some params in headers and a file(optional) in body as multi part. Now when I try to submit with file present, it works just fine, but when the file is not present in the request it says 415 error. below is code request(for submitting I am using Postman in Chrome)

@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@HeaderParam("uid") String userId,
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {
    // save file to disk
    return Response.status(200).entity("Done").build();
}

This is a JAX-RS/Jersey based solution taken from MKYONG tutorial. Let me know if more information is required.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Jayesh
  • 402
  • 1
  • 4
  • 22
  • The request doesn't have a json, it just have uid in header(uid: U1234) and file is missing from the body(i don't want to attach it, as its optional). Server is Apache Tomcat 7.x. Where do i get the stack trace since it is not reaching any where in my code? – Jayesh Aug 08 '15 at 19:25
  • I think this is what you are asking(if i am correct)http://pastebin.com/Zh6z2dKK – Jayesh Aug 08 '15 at 19:28
  • Let me try @WandMaker method. – Jayesh Aug 08 '15 at 19:28

1 Answers1

2

You can create an overloaded method like below which does not take file details as inputs and not have @Consumes(MediaType.MULTIPART_FORM_DATA) for it.

As per JAX-RS documentation, content-type of request, specified through @Consumes, plays a role in resolution of overloaded method to be invoked.

@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(@HeaderParam("uid") String userId) {

    return Response.status(200).entity("Done").build();
}
Wand Maker
  • 18,476
  • 8
  • 53
  • 87