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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
