Spring MVC中的StreamingOutput模拟?

时间:2013-12-28 15:09:55

标签: spring-mvc streaming jax-rs

Spring MVC中是否存在JAX-RS StreamingOutput的类似内容?我最初的任务是释放一个服务线程来写一个大的响应。

1 个答案:

答案 0 :(得分:0)

您可以尝试返回InputStreamResourceas exemplified by Sergey Petunin的实例:

@RequestMapping(value = "/stream", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody
public Resource getStream() {
    String string = "Hello World!";
    // acquiring the stream
    InputStream stream = new ByteArrayInputStream(string.getBytes());
    // counting the length of data
    final long contentLength = string.length();
    return new InputStreamResource(stream){
        @Override
        public long contentLength() throws IOException {
            return contentLength;
        }
    };
}