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...
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
.