Reputation: 21
I wanted to write a Jersey 2 Client that would write the stream of data to POST call chunk by chunk.
Why? , this will help me to avoid to keep the whole inputstream request data to store in disk memory before sending via a POST call.
I have searched over the net and looked into Jersey2 API as well but did not find any solution , however there are solution for server side which sends the huge response in stream and reads the same in Jersey Client by doing a GET call at Client , but I wanted send the huge payload say 1 GB of XML data as stream vis POST call.
I tried using solution given in here , but this solution again uses the system memory.
I do not want to store 1GB of data in disk , instead create on-fly the 1GB request stream of data / write the 1GB data to POST call directly chunk by chunk.
Any help highly appreciated. Thanks in Advance.
Upvotes: 0
Views: 3260
Reputation: 21
After doing much research I found the solution and here it goes :
First we need to implements javax.ws.rs.core.StreamingOutput
as mentioned below:
public class JerseyStreamingOutput implements StreamingOutput {
/**
* Overriding the write method to write request data directly to Jersey outputStream .
* @param outputStream
* @throws IOException
* @throws WebApplicationException
*/
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
// Write to Jersey OutputStream chunk by chunk here
}
}
Jersey Cient code :
JerseyStreamingOutput jerseyStreamingOutput =
new JerseyStreamingOutput();
WebTarget target = client.target("http:localhost:8080");
response = target.path("/somepath").
request().post(Entity.entity(jerseyStreamingOutput, MediaType.APPLICATION_OCTET_STREAM_TYPE));
So the above solution helps in saving hard disk memory by writing the request chunk by chunk to outputStream , without this approach we would end up keeping the request xml file say 1GB in hard disk.
Upvotes: 1