User user = restTemplate.getForObject("http://pack.com/users/{userId}", User.class, 666);默认情况下 , RestTemplate 会注册所有内置的消息转换器 , 这决定于你当前类路径是否有相应的转换库 。你也可以显式设置要使用的消息转换器 。默认构造函数如下:
public RestTemplate() {this.messageConverters.add(new ByteArrayHttpMessageConverter());this.messageConverters.add(new StringHttpMessageConverter());this.messageConverters.add(new ResourceHttpMessageConverter(false));// ...其它转换器if (jackson2Present) {this.messageConverters.add(new MappingJackson2HttpMessageConverter());}else if (gsonPresent) {this.messageConverters.add(new GsonHttpMessageConverter());}else if (jsonbPresent) {this.messageConverters.add(new JsonbHttpMessageConverter());}// ...其它转换器this.uriTemplateHandler = initUriTemplateHandler();}注意:RestTemplate 目前处于维护模式,只接受小改动和错误请求 。请考虑改用 WebClient 。
WebClientWebClient 是执行 HTTP 请求的非阻塞、反应式客户端 。它在 5.0 中引入,提供了 RestTemplate 的替代方案,支持同步、异步和流场景 。
WebClient 支持以下功能:
- 非阻塞 I/O 。
- 反应流反向压力
- 用更少的硬件资源实现高并发 。
- 利用 Java 8 lambdas 的函数式流畅应用程序接口 。
- 同步和异步交互 。
- 服务器上的数据流或服务器下的数据流 。
Mono<Person> result = client.get().uri("/users/{userId}", id).accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(User.class);HTTP InterfaceSpring Framework 可让你将 HTTP 服务定义为一个 Java 接口 , 其中包含用于 HTTP 交换的注解方法 。然后 , 你可以生成一个实现该接口并执行交换的代理 。这有助于简化 HTTP 远程访问 , 因为远程访问通常需要使用一个门面来封装使用底层 HTTP 客户端的细节 。首先 , 声明一个带有 @HttpExchange 方法的接口:
@HttpExchange(url = "/demos")public interface DemoInterface {@PostExchange("/format3/{id}")Users queryUser(@PathVariable Long id);}创建一个代理,执行所声明的 HTTP exchanges:@Servicepublic class DemoService {private final DemoInterface demoInterface ;public DemoService() {// 基于响应式调用;你当前的环境需要引入webfluxWebClient client = WebClient.builder().baseUrl("http://localhost:8088/").build() ;HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build() ;this.demoInterface = factory.createClient(DemoInterface.class) ;}public Users queryUser(Long id) {return this.demoInterface.queryUser(id) ;}}测试接口@Resourceprivate DemoService demoService ;@GetMapping("/{id}")public Users getUser(@PathVariable("id") Long id) {return this.demoService.queryUser(id) ;}执行结果
文章插图
图片
支持的方法参数
说明
URI
动态设置请求的 URL , 覆盖注解的 url 属性 。
HttpMethod
动态设置请求的 HTTP 方法 , 覆盖注解的方法属性
@RequestHeader
添加一个或多个请求标头 。参数可以是包含多个标头的 Map<String, ?> 或 MultiValueMap<String, ?>、值集合<?> 或单个值 。
@PathVariable
添加一个变量,用于扩展请求 URL 中的占位符 。参数可以是包含多个变量的 Map<String, ?> 或单个值 。
@RequestBody
提供请求的正文,既可以是要序列化的对象 , 也可以是 Reactive Streams Publisher(如 Mono、Flux 或通过配置的 ReactiveAdapterRegistry 支持的任何其他异步类型) 。
@RequestParam
添加一个或多个请求参数 。参数可以是包含多个参数的 Map<String, ?> 或 MultiValueMap<String, ?>、数值集合<?> 或单个数值 。
当 "content-type"设置为 "application/x-www-form-urlencoded "时,请求参数将在请求正文中编码 。否则,它们将作为 URL 查询参数添加 。
@RequestPart
添加一个请求部分 , 它可以是字符串(表单字段)、资源(文件部分)、对象(要编码的实体 , 如 JSON)、HttpEntity(部分内容和标头)、Spring 部分或上述任何部分的 Reactive Streams 发布器 。
推荐阅读
- 再有人问你数据库连接池 Druid 的原理,这篇文章甩给他!
- 聊聊分布式数据库TDSQL的技术架构
- 2024年影响安全领域的五大技术趋势
- 互联网大厂是如何设计和使用缓存的?方案已开源!
- NAT协议的实现方式
- 一个GPT的幽灵在Gemini上空徘徊
- 搜索引擎如何判定网站的价值
- 为什么有的网站不更新文章也有很好的排名?
- TikTok独特的短视频美学探究
- 外国人喜欢什么?看看他们的网红就知道了!
