当前位置: 首页 > article >正文

Spring boot3-WebClient远程调用非阻塞、响应式HTTP客户端

来吧,会用就行具体理论不讨论

1、首先pom.xml引入webflux依赖

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

别问为什么因为是响应式.......都说不管理论了,继续 


2、创建WebClientController控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.atguigu.boot3_07_http.service.WebClientService;

@RestController
public class WebClientController {

    @Autowired
    private WebClientService webClientService;// 等下创建真正干活的service类

/**
     * 博主这里对接的是华为云的人脸API获取token的测试,用什么api测试都是大同小异的
     *  WebClient 
     * @param authWrapper 请求体消息,有华为云要的key,id,scecc,password,name啥啥的,根据自己的api要求来就行
     * @return
     */
@PostMapping("/huawei")
    public Mono<String> huawei(@RequestBody(required = false) AuthWrapper authWrapper) {
        if (authWrapper == null) {
            return Mono.just("请求体不能为空"); // 返回错误信息
        }
        return webClientService
                .webClient("https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens", authWrapper) //这里放接口地址和参数,当然authWrapper实际项目是从数据库拿的
                .onErrorResume(e -> {
                    // 处理其他可能的异常
                    return Mono.just("发生错误:" + e.getMessage());
                });
    }

}

3、创建打工仔程序员WebClientService 类

import com.atguigu.boot3_07_http.entity.AuthWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Service
public class WebClientService {

    public Mono<String> webClient(String url, AuthWrapper authWrapper) {
        //远程调用
        //1、创建WebClient
        WebClient client = WebClient.create();

        //2.编辑请求
        return client.post()
                .uri(url)
                // .contentType(MediaType.APPLICATION_JSON) //和.header()一样的不用重复,推荐用.header自定义头部
                .bodyValue(authWrapper) //请求体参数
                .header("Content-Type", "application/json") //Content-Type字段,json格式,具体看api有要求请求头不
                .retrieve()
                //.bodyToMono(String.class) // 获取响应体的JSON数据并转换为String类型的Mono对象
                .toBodilessEntity()  // 应为博主测试的api返回的是响应头,所有不关注响应体
                .map(response -> {
                    String token = response.getHeaders().getFirst("X-Subject-Token");//获取字段X-Subject-Token响应头消息
                    if (token == null) {
                        // 不抛出异常,而是返回错误信息
                        return "错误:未获取到华为云Token";
                    }
                    return token;
                    //或者if去掉,用Objects.requireNonNullElse() //对象不为空输出:参数1,为空输出:参数2
//                    return Objects.requireNonNullElse(token, "错误:未获取到华为云Token");
                });
    }

}

最后看下返回结果,华为云返回的token


好啦,WebClient远程调用非阻塞、响应式HTTP客户端

最后大家有没有觉得,比RestTemplate传统的方便很多,但是没有实现高可用,有新的请求还有要搞一堆,我们使用 Http Interface: 声明式编程(官方推荐,也是很多大型项目的用法),并且创建工厂接口,这样只用写一次,以后需要的不用api可用直接调用,我也同时更新出来了


http://www.kler.cn/a/583283.html

相关文章:

  • Web基础:HTML快速入门
  • 订单超时自动取消的4种实现方案:从定时任务到时间轮
  • wow-rag学习|手搓RAG
  • Spring MVC源码分析のinit流程
  • 【心理课堂】学习软件的道路上若感到了困难和迷茫怎么办
  • ubuntu 24.04通过Flatpak安装迅雷
  • LeetCode2012
  • 【DNS系列】httpdns实现原理
  • Chrome 扩展开发 API实战:History(三)
  • 【蓝桥杯】3514字串简写
  • 豆包大模型 1.5 正式发布,全面上线火山方舟
  • 中国软件供应链安全技术指南|DevSecOps敏捷安全技术金字塔V3.0正式发布
  • MySQL启动报错解决
  • Linux》》Ubuntu22.04下Docker的安装 Docker
  • 【Java代码审计 | 第十一篇】SSRF漏洞成因及防范
  • C# Channel
  • springboot集成neo4j搭建知识图谱后端项目(一)
  • 深度学习基础:线性代数本质5——行列式
  • 【网络安全 | 漏洞挖掘】$15,000——通过持久token获取个人身份信息(PII)
  • 图像识别技术与应用-YOLO