Close
« Tutorial

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?

2. Which Java class can be used to write a Java object into an output stream in its serialized binary form?

3. What must be true about a Java object before it can be written with ObjectOutputStream.writeObject()?

4. Which class on the receiving side reads back a Java object written by ObjectOutputStream?

5. Why is a web browser not a good client for a response written with ObjectOutputStream?

6. What does the handler method below return?

@GetMapping("/data")
public StreamingResponseBody data() {
    return out -> {
        // write to out
    };
}

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);
    }
}

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));

9. What does the client need to do after calling readObject() to use the result as a Map?

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
}

11. Why is a task executor often configured when using StreamingResponseBody?

12. What is the main advantage of using StreamingResponseBody over returning a normal object?

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!");
        }
    };
}
Join