替换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官方文档介绍: