Jersey MessageBodyWriter的流式输出
我知道我可以使用StreamingOutput对输出进行流式处理。但是我可以用一个MessageBodyWriter来做吗?如果我这样实施:Jersey MessageBodyWriter的流式输出,jersey,jax-rs,dropwizard,Jersey,Jax Rs,Dropwizard,我知道我可以使用StreamingOutput对输出进行流式处理。但是我可以用一个MessageBodyWriter来做吗?如果我这样实施: @Override public void writeTo(HelloWorldRepresentation t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, O
@Override
public void writeTo(HelloWorldRepresentation t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
"Hello world".chars().forEach(i -> {
try {
entityStream.write(i);
entityStream.write('\n');
entityStream.flush();
Thread.sleep(1000);
} catch (Exception e) {
throw new WebApplicationException(e);
}
});
}
@覆盖
public void writeTo(HelloWorldT表示,类类型,类型genericType,注释[]注释,
MediaType MediaType、多值Map HttpHeader、OutputStream entityStream)
抛出IOException、WebApplicationException{
“你好,世界”。chars().forEach(i->{
试一试{
entityStream.write(i);
entityStream.write('\n');
entityStream.flush();
睡眠(1000);
}捕获(例外e){
抛出新的WebApplicationException(e);
}
});
}
所有输出似乎在同一时间到达(即不是流式传输)。有什么线索吗?对于偶然发现这个问题的好奇者来说,它不是流媒体,因为没有足够的数据流。标准是
/**
* The default buffer size ({@value}) for I/O operations on byte and character
* streams.
*/
public static final int IO_DEFAULT_BUFFER_SIZE = 8192;
(来自)我显然没有击中。检查这个问题