Spring6.1新特性,四种方式调用REST接口(RestClient、WebClient、RestTemplate、HTTP Interface)
个人博客:无奈何杨(wnhyang)
个人语雀:wnhyang
共享语雀:在线知识共享
Github:wnhyang - Overview
官网
REST Clients :: Spring Framework
The Spring Framework provides the following choices for making calls to REST endpoints:
- RestClient - synchronous client with a fluent API.
- WebClient - non-blocking, reactive client with fluent API.
- RestTemplate - synchronous client with template method API.
- HTTP Interface - annotated interface with generated, dynamic proxy implementation.
RestClient
官方描述:RestClient
是一个同步HTTP
客户端,它提供了一个现代、流畅的API
。它提供了对HTTP
库的抽象,允许从Java
对象到HTTP
请求的方便转换,以及从HTTP
响应创建对象。
Spring6.1
版本新特性。
创建
创建RestClient
非常简单,可以使用静态create
方法,也可以使用builder
创建,其提供了非常丰富的定制化选项,请求工厂、消息转换器、拦截器、默认头、请求初始化器等等,简单易懂。