0
# Spring Web Module
1
2
Spring Web module provides foundational web infrastructure including HTTP abstractions, web client capabilities, servlet integration, reactive web support, and web-related utilities. It serves as the foundation for both Spring MVC (servlet-based) and Spring WebFlux (reactive) web frameworks.
3
4
## Package Information
5
6
- **Package Name**: spring-web
7
- **Group ID**: org.springframework
8
- **Artifact ID**: spring-web
9
- **Package Type**: Maven
10
- **Language**: Java
11
- **Version**: 6.2.8
12
- **Installation**:
13
```xml
14
<dependency>
15
<groupId>org.springframework</groupId>
16
<artifactId>spring-web</artifactId>
17
<version>6.2.8</version>
18
</dependency>
19
```
20
21
## Core Imports
22
23
```java
24
// HTTP abstractions
25
import org.springframework.http.*;
26
import org.springframework.http.client.*;
27
28
// Web clients
29
import org.springframework.web.client.RestTemplate;
30
import org.springframework.web.client.RestClient;
31
32
// Message conversion
33
import org.springframework.http.converter.*;
34
import org.springframework.http.codec.*;
35
36
// Web binding and annotations
37
import org.springframework.web.bind.annotation.*;
38
39
// Reactive web support
40
import org.springframework.web.server.*;
41
import org.springframework.web.reactive.function.client.*;
42
43
// Web utilities
44
import org.springframework.web.util.*;
45
```
46
47
## Basic Usage
48
49
```java
50
import org.springframework.web.client.RestTemplate;
51
import org.springframework.http.*;
52
53
// Create a REST client
54
RestTemplate restTemplate = new RestTemplate();
55
56
// Simple GET request
57
String result = restTemplate.getForObject(
58
"https://api.example.com/users/{id}",
59
String.class,
60
123
61
);
62
63
// POST request with body
64
HttpHeaders headers = new HttpHeaders();
65
headers.setContentType(MediaType.APPLICATION_JSON);
66
HttpEntity<User> request = new HttpEntity<>(user, headers);
67
68
ResponseEntity<User> response = restTemplate.postForEntity(
69
"https://api.example.com/users",
70
request,
71
User.class
72
);
73
74
// Modern fluent API with RestClient
75
RestClient restClient = RestClient.create();
76
User user = restClient.get()
77
.uri("https://api.example.com/users/{id}", 123)
78
.retrieve()
79
.body(User.class);
80
```
81
82
## Architecture
83
84
The Spring Web module is organized around several key architectural components:
85
86
- **HTTP Abstractions**: Core HTTP types (`HttpEntity`, `ResponseEntity`, `HttpHeaders`) for building web applications
87
- **Client Infrastructure**: HTTP client factories and request/response abstractions for outbound communication
88
- **Message Processing**: Converters and codecs for transforming between Java objects and HTTP message bodies
89
- **Web Binding**: Annotation-driven parameter binding and data validation for web endpoints
90
- **Reactive Support**: Non-blocking web server infrastructure for reactive applications
91
- **Integration Layer**: Servlet API integration and web context management
92
93
## Capabilities
94
95
### HTTP Abstractions and Core Types
96
97
Essential HTTP types for building request/response entities, working with headers, status codes, and media types.
98
99
```java { .api }
100
// Core HTTP entity types
101
class HttpEntity<T> {
102
HttpEntity(T body);
103
HttpEntity(MultiValueMap<String, String> headers);
104
HttpEntity(T body, MultiValueMap<String, String> headers);
105
HttpHeaders getHeaders();
106
T getBody();
107
boolean hasBody();
108
}
109
110
class ResponseEntity<T> extends HttpEntity<T> {
111
ResponseEntity(HttpStatusCode status);
112
ResponseEntity(T body, HttpStatusCode status);
113
HttpStatusCode getStatusCode();
114
static ResponseEntity<T> ok(T body);
115
static ResponseEntity<Void> noContent();
116
}
117
118
class RequestEntity<T> extends HttpEntity<T> {
119
RequestEntity(HttpMethod method, URI url);
120
RequestEntity(T body, HttpMethod method, URI url);
121
HttpMethod getMethod();
122
URI getUrl();
123
}
124
```
125
126
[HTTP Abstractions](./http-abstractions.md)
127
128
### HTTP Client Infrastructure
129
130
Comprehensive HTTP client support including RestTemplate, modern RestClient, and client infrastructure.
131
132
```java { .api }
133
// Modern fluent client API
134
interface RestClient {
135
RequestHeadersUriSpec<?> get();
136
RequestBodyUriSpec post();
137
RequestBodyUriSpec put();
138
static RestClient create();
139
static RestClient create(String baseUrl);
140
}
141
142
// Traditional template-based client
143
class RestTemplate implements RestOperations {
144
RestTemplate();
145
<T> T getForObject(String url, Class<T> responseType, Object... uriVariables);
146
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables);
147
<T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables);
148
}
149
```
150
151
[HTTP Clients](./http-clients.md)
152
153
### Message Conversion and Encoding
154
155
HTTP message converters for servlet applications and reactive codecs for streaming applications.
156
157
```java { .api }
158
// Message converter interface
159
interface HttpMessageConverter<T> {
160
boolean canRead(Class<?> clazz, MediaType mediaType);
161
boolean canWrite(Class<?> clazz, MediaType mediaType);
162
List<MediaType> getSupportedMediaTypes();
163
T read(Class<? extends T> clazz, HttpInputMessage inputMessage);
164
void write(T t, MediaType contentType, HttpOutputMessage outputMessage);
165
}
166
167
// Reactive message reader/writer
168
interface HttpMessageReader<T> {
169
boolean canRead(ResolvableType elementType, MediaType mediaType);
170
Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints);
171
Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints);
172
}
173
```
174
175
[Message Conversion](./message-conversion.md)
176
177
### Web Binding and Annotations
178
179
Annotation-driven web development with parameter binding, validation, and controller configuration.
180
181
```java { .api }
182
// Core web annotations
183
@RequestMapping(value = "/api/users", method = RequestMethod.GET)
184
@GetMapping("/api/users/{id}")
185
@PostMapping("/api/users")
186
@RequestParam(value = "name", required = false, defaultValue = "unknown")
187
@PathVariable("id")
188
@RequestBody
189
@ResponseBody
190
191
// Controller configuration
192
@RestController
193
@ControllerAdvice
194
@ExceptionHandler(value = UserNotFoundException.class)
195
@ResponseStatus(HttpStatus.NOT_FOUND)
196
```
197
198
[Web Binding and Annotations](./web-binding.md)
199
200
### Reactive Web Support
201
202
Non-blocking web server infrastructure for building reactive applications with WebFlux.
203
204
```java { .api }
205
// Core reactive web interfaces
206
interface WebHandler {
207
Mono<Void> handle(ServerWebExchange exchange);
208
}
209
210
interface ServerWebExchange {
211
ServerHttpRequest getRequest();
212
ServerHttpResponse getResponse();
213
Map<String, Object> getAttributes();
214
Mono<WebSession> getSession();
215
Principal getPrincipal();
216
}
217
218
// WebFilter for reactive request processing
219
interface WebFilter {
220
Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain);
221
}
222
```
223
224
[Reactive Web Support](./reactive-web.md)
225
226
### Web Utilities and Additional Features
227
228
URI building, HTML escaping, pattern matching, CORS support, and other web utilities.
229
230
```java { .api }
231
// URI building utilities
232
class UriComponentsBuilder implements UriBuilder {
233
static UriComponentsBuilder fromPath(String path);
234
static UriComponentsBuilder fromUri(URI uri);
235
UriComponentsBuilder path(String path);
236
UriComponentsBuilder queryParam(String name, Object... values);
237
URI build(Object... uriVariables);
238
}
239
240
// Web service client support
241
class HttpServiceProxyFactory {
242
static HttpServiceProxyFactory.Builder builder();
243
<S> S createClient(Class<S> serviceType);
244
}
245
```
246
247
[Web Utilities](./web-utilities.md)
248
249
## Common Types
250
251
```java { .api }
252
// HTTP method enumeration
253
enum HttpMethod {
254
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE;
255
boolean matches(String method);
256
static HttpMethod resolve(String method);
257
}
258
259
// HTTP status codes
260
enum HttpStatus implements HttpStatusCode {
261
OK(200), CREATED(201), NO_CONTENT(204),
262
BAD_REQUEST(400), UNAUTHORIZED(401), NOT_FOUND(404),
263
INTERNAL_SERVER_ERROR(500);
264
265
int value();
266
String getReasonPhrase();
267
boolean is2xxSuccessful();
268
boolean is4xxClientError();
269
static HttpStatus valueOf(int statusCode);
270
}
271
272
// Media type representation
273
class MediaType extends MimeType {
274
static final MediaType APPLICATION_JSON;
275
static final MediaType APPLICATION_XML;
276
static final MediaType TEXT_PLAIN;
277
static final MediaType MULTIPART_FORM_DATA;
278
279
MediaType(String type, String subtype);
280
boolean includes(MediaType other);
281
static MediaType parseMediaType(String mediaType);
282
}
283
284
// HTTP headers management
285
class HttpHeaders implements MultiValueMap<String, String> {
286
HttpHeaders();
287
void setAccept(List<MediaType> acceptableMediaTypes);
288
void setContentType(MediaType mediaType);
289
void setContentLength(long contentLength);
290
MediaType getContentType();
291
long getContentLength();
292
}
293
```