如何从弹簧控制器中发送文本块?

时间:2018-02-27 18:35:19

标签: java spring spring-boot

我希望在弹簧控制器准备就绪时以块的形式发送数据。

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

如何实现这个目标?

1 个答案:

答案 0 :(得分:1)

Spring MVC可以提供一个servlet响应对象,然后您可以将其写入。例如。 :

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