[英]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.