在 Jersey 中使用 StreamingOutput 作为响应实体的示例示例、实体、Jersey、StreamingOutput

2023-09-06 10:45:39 作者:菠萝菠萝蜜i

有人可以发布一个示例,说明如何在 Jersey 中将 StreamingOutput 设置为 Response 对象中的实体?

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

我找不到这样的例子.

推荐答案

看看有没有帮助:

@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();
}