or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration.mdfunctional-programming.mdhttp-client.mdindex.mdrequest-processing.mdview-resolution.mdwebsocket.md

index.mddocs/

0

# Spring WebFlux

1

2

Spring 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.

3

4

## Package Information

5

6

- **Package Name**: org.springframework:spring-webflux

7

- **Package Type**: maven

8

- **Language**: Java (with Kotlin support)

9

- **Installation**: Add to your Maven dependencies:

10

11

```xml

12

<dependency>

13

<groupId>org.springframework</groupId>

14

<artifactId>spring-webflux</artifactId>

15

<version>6.2.8</version>

16

</dependency>

17

```

18

19

For Gradle:

20

21

```gradle

22

implementation 'org.springframework:spring-webflux:6.2.8'

23

```

24

25

## Core Imports

26

27

```java

28

import org.springframework.web.reactive.function.client.WebClient;

29

import org.springframework.web.reactive.function.server.RouterFunction;

30

import org.springframework.web.reactive.function.server.ServerResponse;

31

import org.springframework.web.reactive.function.server.RouterFunctions;

32

import org.springframework.web.reactive.function.server.RequestPredicates;

33

import org.springframework.web.reactive.DispatcherHandler;

34

import org.springframework.web.reactive.socket.WebSocketHandler;

35

import org.springframework.web.reactive.config.EnableWebFlux;

36

```

37

38

## Basic Usage

39

40

```java

41

import org.springframework.web.reactive.function.client.WebClient;

42

import org.springframework.web.reactive.function.server.RouterFunction;

43

import org.springframework.web.reactive.function.server.ServerResponse;

44

import org.springframework.web.reactive.function.server.RouterFunctions;

45

import static org.springframework.web.reactive.function.server.RequestPredicates.*;

46

import reactor.core.publisher.Mono;

47

48

// HTTP Client usage

49

WebClient client = WebClient.create("https://api.example.com");

50

Mono<String> response = client.get()

51

.uri("/users/{id}", 123)

52

.retrieve()

53

.bodyToMono(String.class);

54

55

// Functional routing usage

56

RouterFunction<ServerResponse> route = RouterFunctions.route()

57

.GET("/users/{id}", request -> {

58

String id = request.pathVariable("id");

59

return ServerResponse.ok().bodyValue("User: " + id);

60

})

61

.POST("/users", request ->

62

request.bodyToMono(User.class)

63

.flatMap(this::saveUser)

64

.flatMap(user -> ServerResponse.ok().bodyValue(user))

65

)

66

.build();

67

```

68

69

## Architecture

70

71

Spring WebFlux is built around several key components:

72

73

- **Reactive Core**: Built on Project Reactor (Mono/Flux) for non-blocking, asynchronous processing

74

- **Dispatcher Handler**: Central request dispatcher similar to Spring MVC's DispatcherServlet

75

- **Handler Mappings**: Route requests to appropriate handlers based on URL patterns, HTTP methods, etc.

76

- **Handler Adapters**: Adapt different handler types (controllers, functions) to a common interface

77

- **Functional Programming Model**: RouterFunction and HandlerFunction for composable request handling

78

- **WebClient**: Non-blocking HTTP client with fluent API

79

- **WebSocket Support**: Full reactive WebSocket implementation for real-time communication

80

- **View Resolution**: Template engine integration for server-side rendering

81

82

## Capabilities

83

84

### Reactive HTTP Client

85

86

Non-blocking HTTP client with fluent API for making HTTP requests. Supports request/response customization, error handling, and reactive streams.

87

88

```java { .api }

89

interface WebClient {

90

RequestHeadersUriSpec<?> get();

91

RequestHeadersUriSpec<?> head();

92

RequestBodyUriSpec post();

93

RequestBodyUriSpec put();

94

RequestBodyUriSpec patch();

95

RequestHeadersUriSpec<?> delete();

96

RequestHeadersUriSpec<?> options();

97

RequestBodyUriSpec method(HttpMethod method);

98

Builder mutate();

99

static WebClient create();

100

static WebClient create(String baseUrl);

101

static Builder builder();

102

}

103

```

104

105

[HTTP Client](./http-client.md)

106

107

### Functional Web Programming

108

109

Functional programming model using RouterFunction and HandlerFunction for composable request handling without annotations.

110

111

```java { .api }

112

@FunctionalInterface

113

interface RouterFunction<T extends ServerResponse> {

114

Mono<HandlerFunction<T>> route(ServerRequest request);

115

}

116

117

@FunctionalInterface

118

interface HandlerFunction<T extends ServerResponse> {

119

Mono<T> handle(ServerRequest request);

120

}

121

122

interface ServerRequest {

123

HttpMethod method();

124

URI uri();

125

String path();

126

<T> Mono<T> bodyToMono(Class<? extends T> elementClass);

127

<T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference);

128

String pathVariable(String name);

129

Map<String, String> pathVariables();

130

}

131

```

132

133

[Functional Programming](./functional-programming.md)

134

135

### WebSocket Support

136

137

Full reactive WebSocket implementation supporting both client and server-side WebSocket connections with message handling.

138

139

```java { .api }

140

interface WebSocketSession {

141

String getId();

142

Flux<WebSocketMessage> receive();

143

Mono<Void> send(Publisher<WebSocketMessage> messages);

144

boolean isOpen();

145

Mono<Void> close();

146

}

147

148

@FunctionalInterface

149

interface WebSocketHandler {

150

Mono<Void> handle(WebSocketSession session);

151

}

152

153

interface WebSocketClient {

154

Mono<Void> execute(URI url, WebSocketHandler handler);

155

}

156

```

157

158

[WebSocket Support](./websocket.md)

159

160

### Configuration and Setup

161

162

Configuration classes and annotations for setting up WebFlux applications, including CORS, resource handling, and custom components.

163

164

```java { .api }

165

@Target(ElementType.TYPE)

166

@Retention(RetentionPolicy.RUNTIME)

167

@Import(DelegatingWebFluxConfiguration.class)

168

@interface EnableWebFlux {}

169

170

interface WebFluxConfigurer {

171

default void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {}

172

default void addCorsMappings(CorsRegistry registry) {}

173

default void addResourceHandlers(ResourceHandlerRegistry registry) {}

174

default void configureViewResolvers(ViewResolverRegistry registry) {}

175

}

176

```

177

178

[Configuration](./configuration.md)

179

180

### Request Processing Pipeline

181

182

Core framework components for request dispatching, handler mapping, and result processing in reactive web applications.

183

184

```java { .api }

185

class DispatcherHandler implements WebHandler {

186

Mono<Void> handle(ServerWebExchange exchange);

187

}

188

189

interface HandlerMapping {

190

Mono<Object> getHandler(ServerWebExchange exchange);

191

}

192

193

interface HandlerAdapter {

194

boolean supports(Object handler);

195

Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler);

196

}

197

```

198

199

[Request Processing](./request-processing.md)

200

201

### View Resolution and Templating

202

203

View resolution system supporting multiple template engines including FreeMarker and script templates for server-side rendering.

204

205

```java { .api }

206

interface View {

207

Mono<Void> render(Map<String, ?> model, MediaType contentType, ServerWebExchange exchange);

208

}

209

210

interface ViewResolver {

211

Mono<View> resolveViewName(String viewName, Locale locale);

212

}

213

214

interface Rendering {

215

String name();

216

Map<String, Object> modelAttributes();

217

HttpStatusCode status();

218

}

219

```

220

221

[View Resolution](./view-resolution.md)

222

223

## Types

224

225

```java { .api }

226

// Core response types

227

abstract class ServerResponse {

228

HttpStatusCode statusCode();

229

HttpHeaders headers();

230

Mono<Void> writeTo(ServerWebExchange exchange, Context context);

231

232

static BodyBuilder ok();

233

static BodyBuilder status(HttpStatusCode status);

234

static BodyBuilder created(URI location);

235

static BodyBuilder notFound();

236

}

237

238

// WebSocket message types

239

class WebSocketMessage {

240

enum Type { TEXT, BINARY, PING, PONG }

241

Type getType();

242

DataBuffer getPayload();

243

String getPayloadAsText();

244

}

245

246

class CloseStatus {

247

static final CloseStatus NORMAL;

248

static final CloseStatus GOING_AWAY;

249

static final CloseStatus PROTOCOL_ERROR;

250

251

int getCode();

252

String getReason();

253

}

254

255

// Request/Response context

256

interface ServerWebExchange {

257

ServerHttpRequest getRequest();

258

ServerHttpResponse getResponse();

259

Map<String, Object> getAttributes();

260

WebSession getSession();

261

}

262

```