Pramod Karandikar
Pramod Karandikar

Reputation: 5329

Returning file/files in JSON response (Java-Jersey-ReST)

I am working on a use case where I am displaying user's messages on a JSP. Details of the flow are:

I have tried to implement by referring to these SO questions:

However, it's not solving my purpose. I have the following questions:

  1. Is it possible to send message data (like subject, message, message id, etc) along with the attachments (Inputstream) in one response?
  2. If yes, what needs to be the MediaType for @Produces annotation in my resource method? Currently my resource is annotated with @Produces(MediaType.APPLICATION_JSON). Will this work?
  3. How to send the file data in the response?

Any pointers appreciated. TIA.

Upvotes: 1

Views: 8606

Answers (1)

zyexal
zyexal

Reputation: 1579

  1. You can add custom data to the response Header, so yes you are able to send such message data. Add the data to the response Header.

  2. @Produces(MediaType.APPLICATION_JSON) will not work, unless the clients will accept JSON as a file, what they should and will not do ;)

    The correct MediaType depends on what kind of file you want to submit.

    You can use the default MediaType / MIME-Type MediaType.APPLICATION_OCTET_STREAM / application/octet-stream (Is there a “default” MIME type?) but I think it's better to use the correct and exact MIME-Type for your file.

  3. You will find working examples for sending file data with jersey in Input and Output binary streams using JERSEY? - so there is no need to answer this again :)

Hope this was helpful somehow, have a nice day.

Upvotes: 1

Related Questions