Example of using StreamingOutput as Response entity in Jersey

Jersey

Jersey Problem Overview


Can someone post an example of how in Jersey to set StreamingOutput as an entity in a Response object?

I haven't been able to find an example of this.

Jersey Solutions


Solution 1 - Jersey

See if this helps:

@GET
@Produces(MediaType.TEXT_PLAIN)
public Response streamExample() {
  StreamingOutput stream = new StreamingOutput() {
    @Override
    public void write(OutputStream os) throws IOException,
    WebApplicationException {
      Writer writer = new BufferedWriter(new OutputStreamWriter(os));
      writer.write("test");
      writer.flush();  // <-- This is very important.  Do not forget.
    }
  };
  return Response.ok(stream).build();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid LoyView Question on Stackoverflow
Solution 1 - JerseyconditView Answer on Stackoverflow