Spring / Spring AI interview questions
How do you use Spring AI with Spring WebFlux for a reactive AI endpoint?
Spring AI integrates naturally with Spring WebFlux's reactive pipeline. Because LLM streaming returns a Flux<String> or Flux<ChatResponse>, you can return it directly from a WebFlux controller with zero blocking, delivering tokens to the browser as Server-Sent Events (SSE) as fast as the model produces them.
@RestController @RequestMapping("/ai") public class AiStreamController { private final ChatClient chatClient; public AiStreamController(ChatClient.Builder builder) { this.chatClient = builder.build(); } @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<String> stream(@RequestParam String message) { return chatClient.prompt() .user(message) .stream() .content(); } // For full metadata (finish reason, token counts per chunk) @GetMapping(value = "/stream/full", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<ChatResponse> streamFull(@RequestParam String message) { return chatClient.prompt() .user(message) .stream() .chatResponse(); } }
From the browser or curl, the client reads the event stream as tokens arrive. Backpressure is handled by Project Reactor — if the client cannot consume fast enough, the Flux signals backpressure upstream. For SSE with Spring MVC (not WebFlux), SseEmitter combined with Flux.subscribe() and a manual emitter thread achieves the same result, though WebFlux is cleaner.
More Related questions...