本文档概述了 Spring 框架的 Web 技术栈,包括客户端 HTTP 库及其底层基础架构。它涵盖了传统的同步和现代响应式 HTTP 客户端,以及为它们提供支持的客户端连接器实现。有关服务器端 Web 框架,请参阅 核心组件。
Spring 框架提供了一套全面的 Web 技术,用于构建客户端和服务器端 Web 应用程序。客户端技术使应用程序能够向外部服务发出 HTTP 请求,而服务器技术则处理传入的 HTTP 请求。
来源
Spring 提供了一套丰富的 HTTP 抽象,构成了客户端和服务器端 Web 技术的基础。
HttpHeaders 类通过类型安全访问器提供全面的 HTTP 头管理,支持常用头和自定义头。
主要功能
使用示例
来源
Spring 提供了 HttpEntity、RequestEntity 和 ResponseEntity 类来表示带有头和体的 HTTP 消息。
主要功能
来源
Spring 的 URI 处理能力包括通过 UriBuilderFactory 实现的模板扩展、编码和构建。
DefaultUriBuilderFactory 提供
使用示例
来源
Spring MVC 是基于 Servlet API 构建的传统 Servlet Web 框架。它为构建 Web 应用程序和 REST API 提供了全面的编程模型。
WebMvcConfigurationSupport 中的关键组件
处理器映射器(按顺序)
处理器适配器:
异常解析器:
来源
Spring MVC 根据类路径依赖项自动配置 HTTP 消息转换器
WebMvcConfigurationSupport 中的转换器注册逻辑包括
来源
WebMvcConfigurationSupport 提供了 Spring MVC 的主要配置
来源
Spring WebFlux 是 Spring 5.0 中引入的响应式 Web 框架,基于响应式流构建,专为非阻塞应用程序设计。
WebFluxConfigurationSupport 中的关键组件
处理器映射器:
处理器适配器:
结果处理器:
来源
WebClient 是响应式 HTTP 客户端,提供了 RestTemplate 的非阻塞替代方案
WebClient 实现中的关键类
使用示例
来源
WebFlux supports functional routing through RouterFunction and HandlerFunction
来源
Spring's method invocation system handles the calling of handler methods in both MVC and WebFlux frameworks.
Spring provides extensive argument resolver support for different parameter types
Common resolvers:
@RequestParam - Query/form parameters@PathVariable - URL path variables@RequestBody - Request body deserialization@RequestHeader - HTTP headersHttpServletRequest/ServerRequest - Native request objectsReactive-specific resolvers:
Mono<T> and Flux<T> parameter typesCustom resolver registration:
The ReactiveTypeHandler enables Spring to work with various reactive types
Mono<T>, Flux<T>Single<T>, Observable<T>, Flowable<T>CompletableFuture<T>ReactiveAdapterRegistryExample handler method
来源
While server-side frameworks handle incoming requests, Spring also provides robust client technologies for making outbound HTTP requests.
RestTemplate provides a template-based approach for synchronous HTTP operations
关键方法
getForObject(), getForEntity() - GET requestspostForObject(), postForLocation() - POST requestsput(), delete() - PUT and DELETE requestsexchange() - Generic request methodexecute() - Low-level execution with callbacks使用示例
来源
| 方面 | WebClient | RestTemplate | Spring MVC | Spring WebFlux |
|---|---|---|---|---|
| 模型 | 响应式 | 同步 | Servlet-based | 响应式 |
| 多线程 | Non-blocking | 阻塞 | Thread-per-request | 事件循环 |
| Backpressure | 是 | 否 | 否 | 是 |
| 流式传输 | 是 | 有限 | 有限 | 是 |
| 性能 | High concurrency | 中等 | 中等 | High concurrency |
The choice between technologies depends on application requirements
来源
Spring Framework provides two primary HTTP client technologies with different programming models
WebClient is a modern, reactive HTTP client introduced in Spring 5.0 that provides a non-blocking, fluent API for making HTTP requests. It supports both synchronous and asynchronous operations using Reactor's Mono and Flux types.
关键组件
WebClient.Builder: Creates and configures WebClient instancesRequestHeadersSpec/RequestBodySpec: Fluent API for building requestsResponseSpec: Handles response processingExchangeFilterFunction: Intercepts and transforms requests/responsesExchangeFunction: Performs the actual HTTP exchangeClientHttpConnector: Low-level HTTP connection implementation使用示例
来源
RestTemplate is the traditional synchronous HTTP client in Spring that follows a template method pattern. While still supported, it's being replaced by WebClient and RestClient for new applications.
关键组件
ClientHttpRequestFactory: Creates HTTP requests (various implementations)HttpMessageConverter: Converts between Java objects and HTTP request/response bodiesResponseErrorHandler: Handles HTTP errorsClientHttpRequestInterceptor: Intercepts request/response flow使用示例
来源
The HTTP client connectors are the foundation for both WebClient and RestTemplate, providing actual HTTP connection capabilities. Spring offers multiple implementations to support different underlying HTTP client libraries.
The ReactorClientHttpConnector uses Reactor Netty's HTTP client as the underlying implementation. This is the default connector used by WebClient when no specific connector is configured.
特性
来源
The JettyClientHttpConnector uses the Jetty HTTP client. It's an alternative to Reactor Netty when Jetty is already being used in the application.
特性
来源
The JdkClientHttpConnector uses the JDK's built-in HttpClient (available since Java 11). It's useful when you want to avoid external dependencies.
特性
来源
HttpComponentsClientHttpConnector 使用 Apache HttpComponents HttpClient。此连接器对于已使用 Apache HttpComponents 的环境很有用。
特性
来源
Spring 的客户端和服务器端 Web 组件都使用 HTTP 消息转换器来转换 Java 对象与 HTTP 请求/响应正文。
关键转换器
基于文本的转换器
StringHttpMessageConverter:转换字符串MappingJackson2HttpMessageConverter:使用 Jackson 进行 JSON 转换MappingJackson2XmlHttpMessageConverter:使用 Jackson 进行 XML 转换Jaxb2RootElementHttpMessageConverter:使用 JAXB 进行 XML 转换二进制转换器
ByteArrayHttpMessageConverter:原始字节数组ResourceHttpMessageConverter:Spring Resource 对象表单数据转换器
AllEncompassingFormHttpMessageConverter:表单数据和 multipart其他格式
MappingJackson2SmileHttpMessageConverter:Smile 格式(二进制 JSON)MappingJackson2CborHttpMessageConverter:CBOR 格式MappingJackson2YamlHttpMessageConverter:YAML 格式转换器的自动注册在 RestTemplate 和 WebClient 中都会发生,具体取决于类路径中可用的库。
来源
WebClient 是 Spring 应用程序中推荐的 HTTP 客户端。本节介绍其主要功能和用法模式。
WebClient 实例使用构建器模式创建和配置
来源
WebClient 为发起 HTTP 请求提供了流畅的 API
示例
来源
WebClient 提供了多种处理响应的方式
retrieve() 进行正文提取retrieve() 进行实体提取exchangeToMono() 以获得更多控制来源
WebClient 提供了几种处理错误的机制
来源
尽管 WebClient 是新应用程序的首选 HTTP 客户端,但 RestTemplate 仍广泛用于现有应用程序。
创建和配置 RestTemplate
来源
RestTemplate 为每种 HTTP 方法提供了特定的方法
来源
以下是两种主要 HTTP 客户端技术的比较
| 功能 | WebClient | RestTemplate |
|---|---|---|
| 编程模型 | 响应式(非阻塞) | 同步(阻塞) |
| 返回值类型 | Mono/Flux | 直接对象 |
| API 风格 | 流畅的链式 API | 模板方法模式 |
| HTTP 方法 | 统一 API(method() 调用) | 针对不同 HTTP verb 的专用方法 |
| 反压支持 | 是(通过 Reactor) | 否 |
| 错误处理 | onStatus() + 响应式操作符 | ResponseErrorHandler 接口 |
| 过滤器/拦截器 | ExchangeFilterFunction | ClientHttpRequestInterceptor |
| 资源效率 | 对大量连接高效 | 每个请求一个线程(对大量连接效率低下) |
| Java 版本 | Java 8+ | Java 8+ |
| 服务器支持 | 可与任何 HTTP 服务器配合使用 | 可与任何 HTTP 服务器配合使用 |
当需要以下情况时,请使用 WebClient
当满足以下条件时,请使用 RestTemplate
来源
Spring Framework 的 Web 技术栈仍在不断发展
WebClient 是所有新应用程序推荐使用的 HTTP 客户端RestClient,作为 RestTemplate 的现代化替代品,它具有流畅的 API,但仍然是同步操作RestTemplate 仍处于维护模式,未计划添加新功能随着 Spring Framework 的不断发展,重点越来越放在响应式编程模型和对现代 HTTP 协议版本支持上,同时保持对现有应用程序的向后兼容性。
来源