Close
« Tutorial

Spring MVC - StreamingResponseBody MCQ

0/13
0 ✓
0 ✗

1. A handler method needs to write raw bytes directly to the HTTP response as a stream. What should the method return?

2. How is StreamingResponseBody different from ResponseBodyEmitter when writing the response?

3. For which kind of work is StreamingResponseBody preferable?

4. StreamingResponseBody is a functional interface. Which one method does it have?

5. Which return type completes this handler method?

@GetMapping("/data")
public ____ stream() {
    return out -> out.write("Hi".getBytes());
}

6. What will the client receive as the response body?

@GetMapping("/letters")
public StreamingResponseBody letters() {
    return out -> {
        for (String s : new String[] {"A", "B", "C"}) {
            out.write((s + "-").getBytes());
            out.flush();
        }
    };
}

7. Why do we call flush() on the OutputStream while streaming?

8. What should replace the blank so the handler can also control the HTTP status of a streamed response?

@GetMapping("/file")
public ____ download() {
    StreamingResponseBody body = out ->
            out.write("data".getBytes());
    .......
}

9. Returning a StreamingResponseBody is very similar to returning which type for async processing?

10. Which WebMvcConfigurer method lets you set a custom task executor for async request processing?

11. A client calls /test. What does it receive, and how?

@GetMapping("/test")
public StreamingResponseBody handleRequest() {
    return new StreamingResponseBody() {
        @Override
        public void writeTo(OutputStream out)
                throws IOException {
            out.write("A - ".getBytes());
            out.flush();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            out.write("B - ".getBytes());
            out.flush();
        }
    };
}
Join