Return more that one entity in RESTful response?

658 views Asked by At

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?

1

There are 1 answers

4
Sheetal Mohan Sharma On

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(); }