When using streamoutput response in jersey,how to handle exception after I already flush some output?

Viewed 1683

recently I used the interface "StreamingOutput" in jersey to stream my json format response, for example:

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response test(){
    StreamingOutput stream = new StreamingOutput() {

        @Override
        public void write(OutputStream os) throws IOException,
        WebApplicationException {
            Writer writer = new BufferedWriter(new OutputStreamWriter(os));
                for( ...  loop ...) {
                    //response something immediatly
                    writer.write(somthing ...);
                writer.flush();
            }
        }
    };

    return Response.ok(stream).build();
}   

But what if after I "//response something immediately" to client, some exception is thrown.

Beside just pending a special error message after part of normal response, is there any approach to handle this condition??

1 Answers
Related