SpringBoot 3.0 新特性内置声明式HTTP客户端实例详解

作者:mrr 时间:2022-11-25 13:47:20 

http interface

从 Spring 6 和 Spring Boot 3 开始,Spring 框架支持将远程 HTTP 服务代理成带有特定注解的 Java http interface。类似的库,如 OpenFeign 和 Retrofit 仍然可以使用,但 http interface 为 Spring 框架添加内置支持。

什么是声明式客户端

声明式 http 客户端主旨是使得编写 java http 客户端更容易。为了贯彻这个理念,采用了通过处理注解来自动生成请求的方式(官方称呼为声明式、模板化)。通过声明式 http 客户端实现我们就可以在 java 中像调用一个本地方法一样完成一次 http 请求,大大减少了编码成本,同时提高了代码可读性。

举个例子,如果想调用 /tenants 的接口,只需要定义如下的接口类即可

public interface TenantClient {

@GetExchange("/tenants")
 Flux<User> getAll();
}

Spring 会在运行时提供接口的调用的具体实现,如上请求我们可以如 Java 方法一样调用

@Autowired
TenantClient tenantClient;

tenantClient.getAll().subscribe(

);

测试使用

1. maven 依赖

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- For webclient support -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

如下图: 目前官方只提供了非阻塞 webclient 的 http interface 实现,所以依赖中我们需要添加 webflux

SpringBoot 3.0 新特性内置声明式HTTP客户端实例详解

2. 创建 Http interface 类型

需要再接口类上添加 @HttpExchange 声明此类事 http interface 端点

@HttpExchange
public interface DemoApi {

@GetExchange("/admin/tenant/list")
   String list();

方法上支持如 * 解

@GetExchange:  for HTTP GET requests.
@PostExchange:  for HTTP POST requests.
@PutExchange: for HTTP PUT requests.
@DeleteExchange: for HTTP DELETE requests.
@PatchExchange:  for HTTP PATCH requests.

方法参数支持的注解

@PathVariable: 占位符参数.
@RequestBody: 请求body.
@RequestParam: 请求参数.
@RequestHeader: 请求头.
@RequestPart: 表单请求.
@CookieValue: 请求cookie.

3. 注入声明式客户端

通过给 HttpServiceProxyFactory 注入携带目标接口 baseUrl 的的 webclient,实现 webclient 和 http interface 的关联

@Bean
   DemoApi demoApi() {
       WebClient client = WebClient.builder().baseUrl("http://pigx.pigx.vip/").build();
       HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
       return factory.createClient(DemoApi.class);
   }

4. 单元测试调用 http interface

@SpringBootTest
class DemoApplicationTests {
@Autowired
private DemoApi demoApi;

@Test
void testDemoApi() {
demoApi.list();
}
}

基于Spring Boot 2.7、 Spring Cloud 2021 & Alibaba、 SAS OAuth2 一个可支持企业各业务系统或产品快速开发实现的开源微服务应用开发平台

来源:https://blog.csdn.net/qq_16063307/article/details/128129001

标签:SpringBoot,HTTP,客户端
0
投稿

猜你喜欢

  • Mybatis Plugin拦截器开发过程详解

    2021-07-15 22:30:18
  • Spring-IOC容器中的常用注解与使用方法详解

    2021-05-26 23:37:45
  • Android创建与解析XML(二)——详解Dom方式

    2023-08-20 02:30:05
  • Spring Boot启动banner定制的步骤详解

    2023-03-04 19:30:20
  • Java 实战练手项目之酒店管理系统的实现流程

    2022-07-30 09:05:10
  • C# 9.0新特性——只初始化设置器

    2023-03-19 02:31:45
  • java 实现文件复制和格式更改的实例

    2023-10-21 08:07:49
  • VS2022+unity3D开发环境搭建的实现步骤

    2023-02-10 10:13:47
  • Java超细致讲解数组的使用

    2022-07-20 06:25:16
  • 详解如何将JAR包发布到Maven中央仓库

    2023-02-14 07:26:00
  • C#使用反射机制实现延迟绑定

    2021-06-13 22:22:42
  • Scala可变参数列表,命名参数和参数缺省详解

    2022-09-26 21:00:49
  • Unity动画混合树实例详解

    2022-06-17 14:41:27
  • 浅谈Android面向切面编程(AOP)

    2022-04-03 15:14:41
  • 基于html5+java实现大文件上传实例代码

    2023-09-26 02:14:29
  • JAVA8之函数式编程Function接口用法

    2022-01-04 04:03:04
  • Java8如何从一个Stream中过滤null值

    2022-02-03 08:10:20
  • C语言代码实现三子棋小游戏

    2023-04-16 02:12:41
  • jdk15的安装与配置全过程记录

    2023-01-06 05:45:10
  • 浅谈Java锁机制

    2022-01-09 00:58:56
  • asp之家 软件编程 m.aspxhome.com