Spring WebFlux is a reactive web framework that provides a non-blocking, event-driven approach to building web applications and APIs.
npx @tessl/cli install tessl/maven-org-springframework--spring-webflux@6.2.0Spring WebFlux is a reactive web framework that provides a non-blocking, event-driven approach to building web applications and APIs. Built on top of Project Reactor, it supports both functional and annotation-based programming models and is designed for high-concurrency scenarios where applications need to handle thousands of concurrent connections efficiently.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>6.2.8</version>
</dependency>For Gradle:
implementation 'org.springframework:spring-webflux:6.2.8'import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.config.EnableWebFlux;import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.RouterFunctions;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import reactor.core.publisher.Mono;
// HTTP Client usage
WebClient client = WebClient.create("https://api.example.com");
Mono<String> response = client.get()
.uri("/users/{id}", 123)
.retrieve()
.bodyToMono(String.class);
// Functional routing usage
RouterFunction<ServerResponse> route = RouterFunctions.route()
.GET("/users/{id}", request -> {
String id = request.pathVariable("id");
return ServerResponse.ok().bodyValue("User: " + id);
})
.POST("/users", request ->
request.bodyToMono(User.class)
.flatMap(this::saveUser)
.flatMap(user -> ServerResponse.ok().bodyValue(user))
)
.build();Spring WebFlux is built around several key components:
Non-blocking HTTP client with fluent API for making HTTP requests. Supports request/response customization, error handling, and reactive streams.
interface WebClient {
RequestHeadersUriSpec<?> get();
RequestHeadersUriSpec<?> head();
RequestBodyUriSpec post();
RequestBodyUriSpec put();
RequestBodyUriSpec patch();
RequestHeadersUriSpec<?> delete();
RequestHeadersUriSpec<?> options();
RequestBodyUriSpec method(HttpMethod method);
Builder mutate();
static WebClient create();
static WebClient create(String baseUrl);
static Builder builder();
}Functional programming model using RouterFunction and HandlerFunction for composable request handling without annotations.
@FunctionalInterface
interface RouterFunction<T extends ServerResponse> {
Mono<HandlerFunction<T>> route(ServerRequest request);
}
@FunctionalInterface
interface HandlerFunction<T extends ServerResponse> {
Mono<T> handle(ServerRequest request);
}
interface ServerRequest {
HttpMethod method();
URI uri();
String path();
<T> Mono<T> bodyToMono(Class<? extends T> elementClass);
<T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference);
String pathVariable(String name);
Map<String, String> pathVariables();
}Full reactive WebSocket implementation supporting both client and server-side WebSocket connections with message handling.
interface WebSocketSession {
String getId();
Flux<WebSocketMessage> receive();
Mono<Void> send(Publisher<WebSocketMessage> messages);
boolean isOpen();
Mono<Void> close();
}
@FunctionalInterface
interface WebSocketHandler {
Mono<Void> handle(WebSocketSession session);
}
interface WebSocketClient {
Mono<Void> execute(URI url, WebSocketHandler handler);
}Configuration classes and annotations for setting up WebFlux applications, including CORS, resource handling, and custom components.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(DelegatingWebFluxConfiguration.class)
@interface EnableWebFlux {}
interface WebFluxConfigurer {
default void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {}
default void addCorsMappings(CorsRegistry registry) {}
default void addResourceHandlers(ResourceHandlerRegistry registry) {}
default void configureViewResolvers(ViewResolverRegistry registry) {}
}Core framework components for request dispatching, handler mapping, and result processing in reactive web applications.
class DispatcherHandler implements WebHandler {
Mono<Void> handle(ServerWebExchange exchange);
}
interface HandlerMapping {
Mono<Object> getHandler(ServerWebExchange exchange);
}
interface HandlerAdapter {
boolean supports(Object handler);
Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler);
}View resolution system supporting multiple template engines including FreeMarker and script templates for server-side rendering.
interface View {
Mono<Void> render(Map<String, ?> model, MediaType contentType, ServerWebExchange exchange);
}
interface ViewResolver {
Mono<View> resolveViewName(String viewName, Locale locale);
}
interface Rendering {
String name();
Map<String, Object> modelAttributes();
HttpStatusCode status();
}// Core response types
abstract class ServerResponse {
HttpStatusCode statusCode();
HttpHeaders headers();
Mono<Void> writeTo(ServerWebExchange exchange, Context context);
static BodyBuilder ok();
static BodyBuilder status(HttpStatusCode status);
static BodyBuilder created(URI location);
static BodyBuilder notFound();
}
// WebSocket message types
class WebSocketMessage {
enum Type { TEXT, BINARY, PING, PONG }
Type getType();
DataBuffer getPayload();
String getPayloadAsText();
}
class CloseStatus {
static final CloseStatus NORMAL;
static final CloseStatus GOING_AWAY;
static final CloseStatus PROTOCOL_ERROR;
int getCode();
String getReason();
}
// Request/Response context
interface ServerWebExchange {
ServerHttpRequest getRequest();
ServerHttpResponse getResponse();
Map<String, Object> getAttributes();
WebSession getSession();
}