2

I autogenerate the JAX-RS interfaces from Swagger. I use Jersey 2.25.1.

All works fine for most of the use cases. We have the same interface for the server and client parts. Clients are generated from the interface with org.glassfish.jersey.client.proxy.WebResourceFactory.

Now I need to implement file download via streaming (files gonna be huge, typically in the gigabyte range, so streaming is required).

I can use the following signature for the server:

@GET
@Path("/DownloadFile")
@Produces({"application/octet-stream"})
StreamingOutput downloadFileUniqueId();

But StreamingOutput cannot obviously be used in the client.

Is there any feature in JAX-RS / Jersey to have a common interface between server and client ?

I've seen for the upload, this is possible using FormDataMultiPart, I'd like a similar solution for download...

rcomblen
  • 4,579
  • 1
  • 27
  • 32
  • In the client code, you have to read response entity as input stream - `InputStream responseStream = response.readEntity(InputStream.class);` and then read this stream for getting actual file content. – Vikas Sachdeva Oct 12 '17 at 00:41
  • Try using a Response return type, and then in the client code, you can call `response.get(InputStream.class)` – vikarjramun Oct 27 '17 at 19:36

1 Answers1

2

Ok, found a working solution using a javax.ws.rs.core.Response object as return type:

Server code:

public Response downloadFile(String uniqueId){
    InputStream inputStream = filePersistenceService.read(uniqueId);
    Response.ok(outputStream -> IOUtils.copy(inputStream, outputStream)).build()

}

Client code:

Response response = client.downloadFile(uniqueId);
InputStream resultInputStream = response.readEntity(InputStream.class);

This works fine with clients generated by org.glassfish.jersey.client.proxy.WebResourceFactory.

rcomblen
  • 4,579
  • 1
  • 27
  • 32