Springmvc continues to output text to web pages.

  spring-mvc

Background

There are some batch processing things, the background execution may time out, if you wait until you finally encounter an error or have any results, you will often time out, and the details of the entire execution process can’t be seen, it is a black box, not much debugging. Let’s talk about the effect similar to websocket, that is, the backend keeps writing text into the stream.

controller

@Controller
@RequestMapping("/echo")
public class EchoController {

    @RequestMapping(value = "/reply",method = RequestMethod.GET)
    public void writeStream(HttpServletResponse response) throws IOException, InterruptedException {
        response.setContentType("text/html;charset=utf-8");
        for(int i=0;i<1000;i++){
            write(response,"hello");
            Thread.sleep(1000*2);
            System.out.println("send");
        }

        response.getWriter().close();
    }

    private void write(HttpServletResponse response,String content) throws IOException {
        response.getWriter().write(content+"<br/>");
        response.flushBuffer();
        response.getWriter().flush();
    }
}

run

➜  ~ wget http://localhost:8080/echo/reply
--2017-06-14 16:08:14--  http://localhost:8080/echo/reply
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:8080... connected.
HTTP request sent, awaiting response... 200
Length: unspecified [text/html]
Saving to: 'reply'

reply                   [     <=>            ]      60  5.99 B/s

Then tail

➜  ~ tail -f reply
hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>

Or you can use the browser to visit and see the effect.

Curl doesn’t seem to see the effect directly.


For the latest content, please pay attention to WeChat public number

图片描述