我希望在弹簧控制器准备就绪时以块的形式发送数据。
@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);
}
}
如何实现这个目标?
答案 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);
}
}