Spring Boot Spring Boot WebFlux and Reactive Programming 1 — Questions and Answers
Question 1: Which Spring Boot starter is required to use Spring WebFlux?
- spring-boot-starter-web
- spring-boot-starter-webflux (Correct answer)
- spring-boot-starter-reactor
- spring-boot-starter-async
Correct answer: spring-boot-starter-webflux
The `spring-boot-starter-webflux` dependency pulls in Project Reactor and Netty to enable reactive web support in Spring Boot.
Question 2: What is the reactive equivalent of Spring MVC's RestTemplate in Spring WebFlux?
- AsyncRestTemplate
- WebClient (Correct answer)
- HttpClient
- ReactiveTemplate
Correct answer: WebClient
WebClient is the non-blocking, reactive HTTP client introduced in Spring 5 that replaces the deprecated RestTemplate for reactive applications.
Question 3: Which two types does Project Reactor provide for reactive streams in Spring WebFlux?
- Observable and Single
- Flux and Mono (Correct answer)
- Publisher and Subscriber
- Stream and Optional
Correct answer: Flux and Mono
Project Reactor provides `Mono` (0 or 1 element) and `Flux` (0 to N elements) as the core reactive types used in Spring WebFlux.
Question 4: What default embedded server does Spring WebFlux use instead of Tomcat?
- Jetty
- Undertow
- Netty (Correct answer)
- GlassFish
Correct answer: Netty
Spring WebFlux defaults to Netty as the embedded server because it is event-loop-based and designed for non-blocking I/O.
Question 5: Which annotation is used to define a functional-style route handler bean in Spring WebFlux?
- @RequestMapping
- @RouterFunction (Correct answer)
- @RouteHandler
- @WebFluxRoute
Correct answer: @RouterFunction
`RouterFunction` is the functional interface used with `RouterFunctions.route()` to declare routes without annotation-based controllers in WebFlux.
Question 6: What does the `@EnableWebFlux` annotation do when added to a configuration class?
- Enables Tomcat for reactive support
- Imports the WebFlux configuration from DelegatingWebFluxConfiguration (Correct answer)
- Registers all reactive repositories
- Disables Spring MVC auto-configuration
Correct answer: Imports the WebFlux configuration from DelegatingWebFluxConfiguration
`@EnableWebFlux` imports the Spring WebFlux configuration through `DelegatingWebFluxConfiguration`, analogous to `@EnableWebMvc` for the servlet stack.
Which Spring Boot starter is required to use Spring WebFlux?