78

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.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
David Loy
  • 963
  • 1
  • 8
  • 14

1 Answers1

130

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();
}
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
condit
  • 10,852
  • 2
  • 41
  • 60
  • 31
    AAAAARRRRRRRRRRRRRRRRRRRRGH. I just spent four hours trying to figure out why my jersey rest service wasn't streaming a file. I didn't put a flush on my writer. Sooooooo annoying. Thank you for rescuing me! – Will Dec 30 '12 at 22:54
  • 2
    Unfortunately, neither the JAX-RS V 1.1 spec nor the Java EE 6 Javadocs specify if the given output stream should be closed or not. Considering the preceeding comment though, I suspect you should indeed call OutputStream.close() instead of OutputStream.flush(). This may free some internal resources which are associated with the output stream. – Christian Schlichtherle Jul 31 '13 at 09:39
  • Can `MessageBodyWriter` be an alternative? – 卢声远 Shengyuan Lu Jul 25 '14 at 08:16
  • 1
    @卢声远ShengyuanLu You can also use a `MessageBodyWriter` but this question was about streaming with a `Response` object. With `MessageBodyWriter` you're obligated to write to an `OutputStream`. – condit Jul 25 '14 at 14:30
  • 4
    Thanks for this answer, but how the heck do I get this StreamingOutput object out of the Response on the client side?? – koem Nov 30 '14 at 08:25
  • What is the advantage of this? Even when the client is not reading this writes the output which onto the connection. Can this write be called based upon the read from the client. – PrabhaT Apr 02 '15 at 20:52
  • 1
    How do you read the output on client side? Everywhere I see examples of server side!!! :( – prongs May 14 '15 at 11:39
  • @koem If you use a proxy client with Jersey or CXF then Jersey and CXF procudes a StreamingOutput proxy object for you. So simply call the write method on the returned StreamingOutput object and provide an OutputStream it can write the bytes on. – NagyI May 15 '15 at 11:08
  • 3
    @prongs + Nagyl: Yeah, I figured it out: InputStream is = ClientBuilder.newBuilder().register(MultiPartFeature.class).build().target(BASE_URI).path("/yourservice").request().get().readEntity(InputStream.class); (all in one "sentence", it might be good to split this up into Client, Target and Response object) – koem May 16 '15 at 11:39
  • @ChristianSchlichtherle: JAX-RS would be responsible for actually closing the output stream I would think. – cjackson Feb 08 '17 at 05:36
  • @ChristianSchlichtherle: Looking at Glassfish 2.25.1 source (`WriterInterceptorExecutor.TerminalWriterInterceptor.invokeWriteTo()`), I see this code: `entityStream = new UnCloseableOutputStream(context.getOutputStream(), writer);`. So I guess the answer no -- do not close. – kevinarpe Jul 15 '17 at 10:56
  • put that flush in a finally. – Jeryl Cook May 19 '21 at 18:29