【问题标题】:Streaming REST API流式 REST API
【发布时间】:2016-08-03 15:11:47
【问题描述】:

我们目前正在使用 SpringBoot 来实现以 JSON 形式发送响应的 REST 服务。我正在探索“流式传输”响应的方法。 有人可以建议不同的方式/方法来流式传输响应。

问候, 罗希特

【问题讨论】:

标签: rest spring-boot


【解决方案1】:

下面是代码 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();
                    }
                }
            }
        };
    }

【讨论】:

    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2021-02-26
    • 2017-05-12
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多