网站首页 > 技术文章 正文
我们在日常开发中,经常会需要远程调用其他服务提供的接口,比较常用的 HTTP 远程代理框架有OpenFeign、Retrofit以及一些第三方封装工具类,例如Hutool提供的HttpUtil。
11月24日,Spring Boot 3正式发布,Spring官方已经自身支持使用声明式服务调用的方式来调用远程接口。
虽然类似的远程调用框架如OpenFeign和Retrofit仍然可以使用,但HttpServiceProxyFactory增加了对 Spring 框架的原生支持。如果Spring本身可以做到远程调用的话,这些大量的第三方库应该很快会被原生方法取代,我们今天来了解一下这个新特征。
声明式 Http 接口
声明性 HTTP 接口可以让你像定义Java接口那样定义HTTP服务,用法和你平时写Controller中方法完全一致。
引入
声明性 HTTP 接口功能是spring-web依赖项的一部分,使用前必须引入如下依赖包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- For reactive support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
创建 HTTP 服务接口
在 Spring 中,HTTP 服务接口是一个带有@HttpExchange方法的 Java 接口。注释方法被视为 HTTP 端点,细节通过注释属性和输入方法参数类型静态定义。
支持的注解类型
- @HttpExchange:是用于指定 HTTP 端点的通用注释。在接口级别使用时,它适用于所有方法。
- @GetExchange:为 HTTP GET请求指定@HttpExchange。
- @PostExchange:为 HTTP POST请求指定@HttpExchange。
- @PutExchange:为 HTTP PUT请求指定@HttpExchange。
- @DeleteExchange:为 HTTP DELETE请求指定@HttpExchange。
- @PatchExchange:为 HTTP PATCH请求指定@HttpExchange。
方法参数
返回值
声明性 HTTP 接口支持以下返回值:
使用示例
@PutExchange
void update(@PathVariable Long id, @RequestBody User user);
完整使用案例
我们以一个简单的用户信息请求为例
0、构建HttpServiceProxyFactory:
HttpServiceProxyFactory是一个从 HTTP 服务接口创建客户端代理的工厂类。使用HttpServiceProxyFactory.builder(client).build()方法来获取代理 bean 的实例。
import com.fasterxml.jackson.databind.ObjectMapper;
import com.howtodoinjava.app.web.UserClient;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@Configuration
public class WebConfig {
@Bean
WebClient webClient(ObjectMapper objectMapper) {
return WebClient.builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.build();
}
@SneakyThrows
@Bean
UserClient postClient(WebClient webClient) {
HttpServiceProxyFactory httpServiceProxyFactory =
HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient))
.build();
return httpServiceProxyFactory.createClient(UserClient.class);
}
}
1、定义一个简单的用户信息实体类:
public class User {
private int id;
private String username;
private String password;
// 省略
}
2、请求接口:
import com.howtodoinjava.app.model.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.service.annotation.DeleteExchange;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
import org.springframework.web.service.annotation.PostExchange;
import org.springframework.web.service.annotation.PutExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@HttpExchange(url = "/users", accept = "application/json", contentType = "application/json")
public interface UserClient {
@GetExchange("/")
Flux<User> getAll();
@GetExchange("/{id}")
Mono<User> getById(@PathVariable("id") Long id);
@PostExchange("/")
Mono<ResponseEntity<Void>> save(@RequestBody User user);
@PutExchange("/{id}")
Mono<ResponseEntity<Void>> update(@PathVariable Long id, @RequestBody User user);
@DeleteExchange("/{id}")
Mono<ResponseEntity<Void>> delete(@PathVariable Long id);
}
3、将UserClient bean 注入应用程序类并调用方法来获取 API 响应:
@Autowired
UserClient userClient;
//Get All Users
userClient.getAll().subscribe(
data -> log.info("User: {}", data)
);
//Get User By Id
userClient.getById(1L).subscribe(
data -> log.info("User: {}", data)
);
//Create a New User
userClient.save(new User(null, "Lokesh", "lokesh", "admin@email.com"))
.subscribe(
data -> log.info("User: {}", data)
);
//Delete User By Id
userClient.delete(1L).subscribe(
data -> log.info("User: {}", data)
);
完工,不需要定义方法实现就能进行远程HTTP调用,非常方便!
扩展
1、Spring Boot3 新特征一览:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Release-Notes
2、文章相关代码:
https://github.com/lokeshgupta1981/Spring-Boot3-Demos/tree/main/declarative-http-client-example
3、Spring官方文档介绍:
https://docs.spring.io/spring-framework/docs/6.0.0-RC1/reference/html/integration.html#rest-http-interface
原文链接:
https://mp.weixin.qq.com/s/olB4VkuZMMkZ6fVAhyKo-g
- 上一篇: GitHub和码云上,7个h5页面制作工具推荐
- 下一篇: 一篇文章教会你前端中的深拷贝和浅拷贝
猜你喜欢
- 2025-05-26 GitHub和码云上,7个h5页面制作工具推荐
- 2024-09-25 Farm 火了!比 Rspack/Vite 更快的打包方案!
- 2024-09-25 京东通天塔前端性能优化实践 京东通天塔页面
- 2024-09-25 如果你是前端开发程序员,没用过这些插件、工具、类库就out了!
- 2024-09-25 Util应用框架前端概述 前端ui工具
- 2024-09-25 推荐一个vue插件,基于hiprint封装的可视化报表设计与打印工具
- 2024-09-25 如何快速将你的应用封装成JS-SDK?
- 2024-09-25 炫酷数据可视化大屏,前端程序员看过都会打包带走
- 2024-09-25 前端开发React18 - 打包 react前端框架介绍
- 2024-09-25 前端vue打包后部署方式 vue 打包后
你 发表评论:
欢迎- 493℃几个Oracle空值处理函数 oracle处理null值的函数
- 488℃Oracle分析函数之Lag和Lead()使用
- 485℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 472℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 467℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 462℃【数据统计分析】详解Oracle分组函数之CUBE
- 445℃Oracle有哪些常见的函数? oracle中常用的函数
- 439℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 最近发表
-
- Spring Boot跨域难题终结者:3种方案,从此告别CORS噩梦!
- 京东大佬问我,SpringBoot为什么会出现跨域问题?如何解决?
- 在 Spring Boot3 中轻松解决接口跨域访问问题
- 最常见五种跨域解决方案(常见跨域及其解决方案)
- Java Web开发中优雅应对跨域问题(java跨域问题解决办法)
- Spring Boot解决跨域最全指南:从入门到放弃?不,到根治!
- Spring Boot跨域问题终极解决方案:3种方案彻底告别CORS错误
- Spring Cloud 轻松解决跨域,别再乱用了
- Github 太狠了,居然把 "master" 干掉了
- IntelliJ IDEA 调试 Java 8,实在太香了
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端react (48)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端富文本编辑器 (47)
- 前端路由 (55)
- 前端数组 (65)
- 前端定时器 (47)
- Oracle RAC (73)
- oracle恢复 (76)
- oracle 删除表 (48)
- oracle 用户名 (74)
- oracle 工具 (55)
- oracle 内存 (50)
- oracle 导出表 (57)
- oracle 中文 (51)
- oracle链接 (47)
- oracle的函数 (57)
- 前端调试 (52)
- 前端登录页面 (48)
本文暂时没有评论,来添加一个吧(●'◡'●)