Skip to content
Ameen Abozaid
Planned lessonJNR-056-L04

Streaming responses

  • Lesson 4 of 7
  • Junior
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

send text to the client as it is produced.

Planned topics

  • A long reply can take seconds. Streaming sends pieces as they are generated.
  • Spring AI offers .stream().content(), which returns a reactive Flux<String> (one piece at a time).
  • A Spring MVC endpoint can return it as server-sent events (text/event-stream); verify the exact setup before recording.
  • Users see the first words sooner, though total time is the same.
  • Structured output is awkward while streaming; stream plain text, validate whole results.
Draft outline details

Demonstration

java
@GetMapping(value = "/describe/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<String> stream(@RequestParam String name) {
    return chatClient.prompt().user("Describe " + name).stream().content();
}

Check your understanding

What type carries the pieces?

Show answer

A Flux<String>.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.