替换OpenFeign,Spring 新版本自带的 HTTP 客户端工具
原文链接:替换OpenFeign,Spring 新版本自带的 HTTP 客户端工具来了!
声明式Http接口
声明性 HTTP 接口可以让你像定义Java接口那样定义HTTP服务,用法和你平时写Controller中方法完全一致。
一、引入
<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>
二、支持的注解类型
- @HttpExchange:是用于指定
HTTP
端点的通用注释。在接口级别使用时,它适用于所有方法。 - @GetExchange:为
HTTP GET
请求指定@HttpExchange
。 - @PostExchange:为
HTTP POST
请求指定@HttpExchange
。 - @PutExchange:为
HTTP PUT
请求指定@HttpExchange
。 - @DeleteExchange:为
HTTP DELETE
请求指定@HttpExchange
。 - @PatchExchange:为
HTTP PATCH
请求指定@HttpExchange
。
三、参数和返回值
四、示例
1.构建HttpServiceProxyFactory
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); } }
2.接口
实体类
public class User { private int id; private String username; private String password; // 省略 }
接口
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);
调用
@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) );
五、拓展
1、Spring Boot3 新特征一览:
2、文章相关代码:
3、Spring官方文档介绍:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~