1. A handler method needs to write raw bytes directly to the HTTP response as a stream. What should the method return?
Why: When you want to write raw streaming bytes to the output, the handler method should return a StreamingResponseBody.
2. How is StreamingResponseBody different from ResponseBodyEmitter when writing the response?
Why:ResponseBodyEmitter writes through an HttpMessageConverter. StreamingResponseBody skips that step and writes directly to the response OutputStream.
3. For which kind of work is StreamingResponseBody preferable?
Why: Because it writes raw bytes directly to the output stream, it is a good fit for streaming videos or large files.
4.StreamingResponseBody is a functional interface. Which one method does it have?
Why: The only method is writeTo(OutputStream). It is a callback where you write the data to the response body. It can throw IOException.
5. Which return type completes this handler method?
@GetMapping("/data")
public ____ stream() {
return out -> out.write("Hi".getBytes());
}
Why: The lambda takes an OutputStream and may throw IOException. This matches the single method of StreamingResponseBody.
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();
}
};
}
Why: Each loop step writes the letter followed by a dash. The bytes are written in order, so the body is A-B-C-.
7. Why do we call flush() on the OutputStream while streaming?
Why: Flushing pushes the buffered bytes to the client immediately, so the client can see the data piece by piece 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());
.......
}
Why:StreamingResponseBody can be used as the body of a ResponseEntity. This lets you set the status code and headers together with the stream.
9. Returning a StreamingResponseBody is very similar to returning which type for async processing?
Why: Like with Callable, the handler just returns an object. Spring takes care of the thread management and runs the code asynchronously.
10. Which WebMvcConfigurer method lets you set a custom task executor for async request processing?
Why:configureAsyncSupport gives you an AsyncSupportConfigurer. You can call setTaskExecutor() on it to use your own thread pool for async work.
11. A client calls /test. What does it receive, and how?
Why: The code writes A - and flushes it, so the client gets it right away. After a 50 ms pause, it writes and flushes B - . The final body is A - B - , delivered in two parts.