Spring MVC - Writing Java Objects to StreamingResponseBody MCQ
0/13
0✓
0✗
1. Which return type lets a Spring MVC handler method write data directly to the response output stream?
Why:StreamingResponseBody gives you the raw OutputStream of the response, so you can write any data to it directly.
2. Which Java class can be used to write a Java object into an output stream in its serialized binary form?
Why:ObjectOutputStream wraps another output stream and writes Java objects to it using Java serialization.
3. What must be true about a Java object before it can be written with ObjectOutputStream.writeObject()?
Why: Java serialization only works for objects whose class implements Serializable. Otherwise a NotSerializableException is thrown.
4. Which class on the receiving side reads back a Java object written by ObjectOutputStream?
Why:ObjectInputStream is the matching class. It reads serialized data and rebuilds the original Java object.
5. Why is a web browser not a good client for a response written with ObjectOutputStream?
Why: Java serialization is a Java-specific binary format. A browser cannot display or decode it, so a Java client is needed to read it back.
6. What does the handler method below return?
@GetMapping("/data")
public StreamingResponseBody data() {
return out -> {
// write to out
};
}
Why:StreamingResponseBody has a single method that takes an OutputStream. The lambda is run later by Spring, and the stream it receives is the response output stream.
7. What is the best way to make sure the stream is closed after writing an object?
out -> {
// Add the missing part
{
oos.writeObject(list);
}
}
Why: A try-with-resources statement closes the ObjectOutputStream automatically, even if an exception happens while writing.
8. A handler writes the object below. What will the client print after reading the object and looping over the map?
// Server
Map<String, Integer> map = new LinkedHashMap<>();
map.put("a", 1);
map.put("b", 2);
oos.writeObject(map);
// Client
Map<String, Integer> m =
(Map<String, Integer>) ois.readObject();
m.forEach((k, v) ->
System.out.println(k + "=" + v));
Why: The map is serialized and read back with the same entries and the same order (LinkedHashMap keeps insertion order). Each entry is printed as key=value.
9. What does the client need to do after calling readObject() to use the result as a Map?
Why:readObject() returns a plain Object. The client must cast it to the expected type.
10. Which configuration class method lets you set a custom thread pool for async request processing in Spring MVC?
@EnableWebMvc
@Configuration
public class WebConfig
implements WebMvcConfigurer {
// Add the method here
}
Why:configureAsyncSupport() receives an AsyncSupportConfigurer where you can set the task executor used for async request processing.
11. Why is a task executor often configured when using StreamingResponseBody?
Why: Spring runs the streaming code asynchronously. A configured task executor lets you control the threads used, instead of relying on a simple default executor.
12. What is the main advantage of using StreamingResponseBody over returning a normal object?
Why: With StreamingResponseBody you write directly to the output stream. This is useful for large data or any custom format, since nothing needs to be fully built in memory first.
13. What code should go in the blank so the string is written to the response?
@GetMapping("/test")
public StreamingResponseBody handle() {
return out -> {
try (ObjectOutputStream oos = ________) {
oos.writeObject("Hello World!");
}
};
}
Why:ObjectOutputStream is created with its constructor and wraps the OutputStream it is given. Here that is the response stream passed to the lambda. ObjectOutputStream has no static get() method, BufferStream is not a standard Java class, and OutputStream has no stream() method.