I have a REST server that is supposed to send plain text output as a stream, but when I perform the REST call with Postman or Chrome I get the whole output at once at the end of the process instead of getting a stream.
Here is my REST server, inspired from this article:
@GET
@Path("/stream-test")
@Produces(MediaType.TEXT_PLAIN)
public Response streamTest(){
StreamingOutput stream = new StreamingOutput() {
@Override
public void write(OutputStream os) throws IOException, WebApplicationException {
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
for (int i=1; i<=10; i++) {
writer.write("output " + i + " \n");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
writer.flush();
}
};
return Response.ok(stream).build();
}
The output lines are all displayed at the same time. I would like to see an output each 500msec.
Is there something wrong with my implementation ?
Or are Postman and Chrome unable to display streaming outputs ?
Note: for technical reasons I still use Postman as a Chrome app.