简体   繁体   English

如何从Spring Rest Controller发送流中的文本块?

[英]How to send text chunks in stream from spring rest controller?

I would like to send data in chunks as and when it is ready from spring controller. 我想在Spring Controller准备好时分块发送数据。

 @RequestMapping("/")
    public String home(){
         while(i=0;i<10;i++){
          //send numbers from 0 to 10 in chunks as http response
          //code to send this number as response
          Thread.sleep(50000);
        }
                }

How to achive this ? 如何做到这一点?

Spring MVC can provide a servlet response object, which you can then write to. Spring MVC可以提供一个servlet响应对象,然后您可以将其写入。 Eg : 例如:

    @RequestMapping("/")
    public void home(HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        for (int i = 0; i < 10; i++) {
            writer.println(i);
            writer.flush();
            Thread.sleep(50000);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Spring REST控制器返回流 - Returning a stream from a Spring REST Controller Streaming Rest Api - 从 api 以 stream 的形式向客户端发送数据块 - Streaming Rest Api - Send data in chunks to the client in a stream from the restful api 如何使用Spring REST分段发送大文件(REST客户端) - How to send large file using Spring REST multipart in chunks (REST client) Spring 云 Stream:如何从 REST Z594C103F2C6E04E0C3DAZ8 生成 Kafka 消息? - Spring Cloud Stream: how can I produce a Kafka message from a REST controller? 如何将大型输入流发送到Spring REST服务? - How to send a large input stream to a Spring REST service? 无法使用Rest从角度2向弹簧控制器发送值 - Can't send values from angular 2 to spring controller using Rest 如何发送长期休息控制器Spring Boot列表 - How to send list of Long to rest controller Spring Boot 如何将数据从Ajax发送到Spring Controller - How to send data from ajax to spring controller 如何发送 Stream<string> 作为来自 REST API 的客户的响应?</string> - How to send Stream<String> as response to client from REST API? 发送匿名 object - Rest controller - Z38008DD81C2F4D798Z boot ECF6E0CE8AF1D1 - Send anonymous object - Rest controller - Spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM