【发布时间】:2016-08-03 15:11:47
【问题描述】:
我们目前正在使用 SpringBoot 来实现以 JSON 形式发送响应的 REST 服务。我正在探索“流式传输”响应的方法。 有人可以建议不同的方式/方法来流式传输响应。
问候, 罗希特
【问题讨论】:
-
我认为您的问题已经得到解答,请参见下面的链接stackoverflow.com/questions/37155557/…
标签: rest spring-boot
我们目前正在使用 SpringBoot 来实现以 JSON 形式发送响应的 REST 服务。我正在探索“流式传输”响应的方法。 有人可以建议不同的方式/方法来流式传输响应。
问候, 罗希特
【问题讨论】:
标签: rest spring-boot
下面是代码 sn-p 用于从休息端点流式传输数据。
@RequestMapping(value = "/streams", method = RequestMethod.GET)
public StreamingResponseBody getStreamingResponse () {
return new StreamingResponseBody() {
@Override
public void writeTo (OutputStream out) throws IOException {
for (int i = 0; i < 1000; i++) {
out.write((Integer.toString(i) + " - ")
.getBytes());
out.flush();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}
【讨论】: