Spring MVC
StreamingResponseBody is used for asynchronous request processing where the application can write directly to the response OutputStream.
package com.logicbig.example;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;import java.io.IOException;import java.io.OutputStream;@Controllerpublic class TestController { @RequestMapping("/") public StreamingResponseBody handleRequest () { return new StreamingResponseBody() { @Override public void writeTo (OutputStream out) throws IOException { for (int i = 0; i < 1000; i++) { out.write((Integer.toString(i) + " - ") .getBytes()); out.flush(); try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } } }; }}
StreamingResponseBody can be used with ResponseEntity:
package com.logicbig.example;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;import java.io.IOException;import java.io.OutputStream;@Controllerpublic class TestController2 { @RequestMapping("/test") public ResponseEntity<StreamingResponseBody> handleRequest () { StreamingResponseBody responseBody = new StreamingResponseBody() { @Override public void writeTo (OutputStream out) throws IOException { for (int i = 0; i < 1000; i++) { out.write((Integer.toString(i) + " - ") .getBytes()); out.flush(); try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } } }; return new ResponseEntity(responseBody, HttpStatus.OK); }}
package com.logicbig.example;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;import javax.servlet.http.HttpServletRequest;import java.io.IOException;import java.io.OutputStream;import java.util.concurrent.Callable;@Controllerpublic class MyWebController2 { @RequestMapping("/test") public StreamingResponseBody handleRequest (HttpServletRequest r) { System.out.println("asyncSupported: "+ r.isAsyncSupported()); System.out.println(Thread.currentThread().getName()); return new StreamingResponseBody() { @Override public void writeTo (OutputStream outputStream) throws IOException { System.out.println(Thread.currentThread().getName()); outputStream.write("from test request".getBytes()); } }; }}
package com.logicbig.example;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;import java.io.ObjectOutputStream;import java.math.BigInteger;import java.util.HashMap;import java.util.Map;@Controllerpublic class MyController { @RequestMapping("/") public StreamingResponseBody handleRequest () { return outputStream -> { Map<String, BigInteger> map = new HashMap<>(); map.put("one", BigInteger.ONE); map.put("ten", BigInteger.TEN); try(ObjectOutputStream oos = new ObjectOutputStream(outputStream)){ oos.writeObject(map); } }; }}