Close
« Tutorial

Spring MVC - HTTP Streaming MCQ

0/13
0 ✓
0 ✗

1. What can a handler method do by returning a ResponseBodyEmitter?

2. How is each object sent through a ResponseBodyEmitter written to the HTTP response?

3. A client calls this handler. What body does it receive?

@GetMapping("/test")
public ResponseBodyEmitter handle() {
    ResponseBodyEmitter emitter =
            new ResponseBodyEmitter();
    Executors.newSingleThreadExecutor()
            .execute(() -> {
        try {
            for (int i = 1; i <= 3; i++) {
                emitter.send(i + "-",
                        MediaType.TEXT_PLAIN);
            }
            emitter.complete();
        } catch (Exception e) {
            emitter.completeWithError(e);
        }
    });
    return emitter;
}

4. Why should we call complete() on the emitter after the last send()?

5. An error happens while data is being sent. Which call finishes the response because of that error?

6. Data is usually sent to the emitter from a separate thread. Why?

7. Why would a handler return ResponseEntity<ResponseBodyEmitter> instead of a plain ResponseBodyEmitter?

8. Which statement returns the emitter with the HTTP status 202 (Accepted)?

@GetMapping("/data")
public ResponseEntity<ResponseBodyEmitter>
        handle() {
    ResponseBodyEmitter emitter =
            new ResponseBodyEmitter();
    return ...;
}

9. What does the second argument specify in this call?

emitter.send("Hello", MediaType.TEXT_PLAIN);

10. When does a client see the objects sent by a ResponseBodyEmitter?

Join