来自MessageBodyWriter

时间:2017-07-21 07:13:06

标签: jersey jax-rs dropwizard

我知道我可以使用StreamingOutput来传输输出。但是我可以使用MessageBodyWriter吗?如果我这样实现它:

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

所有输出似乎同时到达(即不流式传输)。有线索吗?

1 个答案:

答案 0 :(得分:0)

对于那个偶然发现这个问题的好奇心,因为没有足够的数据来流式传输,所以它不会流式传输。标准是

/**
 * The default buffer size ({@value}) for I/O operations on byte and character
 * streams.
 */
public static final int IO_DEFAULT_BUFFER_SIZE = 8192;

(来自https://github.com/jersey/jersey/blob/master/core-common/src/main/java/org/glassfish/jersey/message/MessageProperties.java),我显然没有参加。