0

I'm developing a RESTful websevices project, my question is simple, is there a way to return both 'File' and 'JSON' entities in the same response?

e.g.: suppose I have this method:

    @GET
        @Path("downloadFile")
        @Consumes(MediaType.TEXT_PLAIN)
        public Response downloadLogStream( ..... ) {
        .....
        Response.ok(resultFile);
        }

but I need to return another entity beside the file itself without adding additional Headers.

is that possible?

Ghayth
  • 894
  • 2
  • 7
  • 18

1 Answers1

0

You can send only response but that can be a complex object. Wrap result/json and status in response.

return Response.status(200).entity(result).build();

More here

@GET
@Path("downloadFile")
@Consumes(MediaType.TEXT_PLAIN)
public Response downloadLogStream( ..... ) {
         // Assuming result is json string
         String result = " JSON is "+jsonObj;
        return Response.status(200).entity(result).build();

    }
}

File download

private static final String FILE_PATH = "pathTo:\\filename. Zip"; 
@GET @Path("/get")         
@Produces(MediaType.APPLICATION_OCTET_STREAM) 
public Response getFile() 
  { 
   File file = new File(FILE_PATH);   
   ResponseBuilder response =    Response.ok((Object) file);   
   response.header("Content-Disposition", "attachment; filename=newfile.zip");
   return response.build(); }
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
  • But this way you've returned only a JSON response, I want to send the File entity as well in the same response. – Ghayth Jun 10 '15 at 08:13
  • You should try with file download then? Will add sample code above – Sheetal Mohan Sharma Jun 10 '15 at 08:18
  • OK, you wrote two methods above right, one to return a JSON and another to return a File, what I want is to return BOTH the JSON and the FILE in the same Response of one method. – Ghayth Jun 10 '15 at 09:41
  • Do a get similar to post here - give the path to server or send the file object wrapped inside json. Check http://stackoverflow.com/questions/3938569/how-do-i-upload-a-file-with-metadata-using-a-rest-web-service – Sheetal Mohan Sharma Jun 10 '15 at 10:12