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

WebClient与HTTPInterface远程调用对比

远程调用

API/SDK的区别:

1.WebClient

1.1创建与调用

创建WebClient非常简单:

  • webclientcreate0
  • webclient.create(string baseurl)

还可以使用webclient.builder0 配置更多参数项:

  • uribuilderfactory: 自定义uribuilderfactory, 定义baseurl.
  • defaulturivariables:默认uril变量。
  • defaultheader:每个请求默认头
  • defaultcookie:每个请求默认 cookie.
  • defaultrequest:consumer 自定义每个请求
  • filter:过滤client发送的每个请求
  • exchangestrategies:http 消息 reader/writer 自元 er 自定义。
  • clientconnector:http client库设置。

定义Weather服务类,定义weather方法

post请求需要定义body 根据不同的接口定义各自的请求参数以及请求头header

public Mono<String> weather(String city){//远程调用api//创建webclientString baseurl = "http://apis.juhe.cn/simpleWeather/query?";WebClient client = WebClient.create();//定义发请求方式Map<String,String>map=new HashMap<>();map.put("city",city);Mono<String> mono = client.post().uri("http://apis.juhe.cn/simpleWeather/query?key=251518e073ef6c3c9504dd286c3f6a86&city={city}", map).contentType(MediaType.APPLICATION_JSON)//定义响应内容类型.header("User-Agent", "ikun").retrieve().bodyToMono(String.class);return mono;
}

在Controller类中返回数据

//调用外部api获取天气
@GetMapping("weather")
public Mono<String> weather(@RequestParam(value = "city") String city){Mono<String> weather = weatherService.weather(city);return weather;
}

2.HTTP Interface

2.1导入依赖

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

2.2定义接口

public interface WeatherInterface {@GetExchange(url = "/query",accept = "application/json")public Mono<String> getweather(@RequestParam("city") String city,@RequestParam String key);
}
public Mono<String> weather2(String city){//创建客户端WebClient client = WebClient.builder().baseUrl("http://apis.juhe.cn/simpleWeather").codecs(clientCodecConfigurer -> {clientCodecConfigurer.defaultCodecs().maxInMemorySize(256*1024*1024);}).build();//创建工厂HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();//获取代理对象WeatherInterface weatherapi = factory.createClient(WeatherInterface.class);Mono<String> getweather = weatherapi.getweather(city,"251518e073ef6c3c9504dd286c3f6a86");return getweather;}

从配置文件中取值 ${key}

2.3封装接口配置

@Configuration
public class WebApiConfig {@BeanHttpServiceProxyFactory httpServiceProxyFactory(){//创建客户端WebClient client = WebClient.builder().baseUrl("http://apis.juhe.cn").defaultHeader("user-agent","ikun") //定义默认请求头.codecs(clientCodecConfigurer -> {clientCodecConfigurer.defaultCodecs().maxInMemorySize(256*1024*1024);}).build();//创建工厂HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();return factory;}@BeanWeatherInterface weatherInterface(){WeatherInterface weatherInterface = httpServiceProxyFactory().createClient(WeatherInterface.class);return weatherInterface;}}

weatherservice里可以自动装配WeatherInterface调用

@Autowired
private WeatherInterface weatherInterface;public Mono<String> weather2(String city, @Value("${apikey}")String key){Mono<String> getweather = weatherInterface.getweather(city,key);return getweather;}

 

http://www.dtcms.com/a/270341.html

相关文章:

  • 第8章:应用层协议HTTP、SDN软件定义网络、组播技术、QoS
  • SPI / I2C / UART 哪个更适合初学者?
  • 通过“逆向侦测”驾驭涌现复杂性的认知架构与技术实现
  • 短视频矩阵管理平台的崛起:源头厂商的深度解析
  • C# Type.GetProperties() 获取不到值的笔记
  • SQL注入与防御-第六章-2:利用操作系统--执行操作系统命令
  • 图像梯度处理与边缘检测:OpenCV 实战指南
  • 【牛客刷题】小红的v三元组
  • FastAPI Docker环境管理脚本使用指南
  • 虚拟机忘记密码怎么办
  • nmon使用方法
  • 征程 6|工具链量化简介与代码实操
  • 云原生安全观察:零信任架构与动态防御的下一代免疫体系
  • 人物设定一秒入魂!RAIDEN-R1提出可验证奖励新范式,让CoT推理更“人格一致”
  • SpringAI学习笔记-MCP客户端简单示例
  • python采集商品详情数据接口json数据返回参考
  • 前端面试常考题目详解​
  • 解决阿里云ubuntu内存溢出导致vps死机无法访问 - 永久性增加ubuntu的swap空间 - 阿里云Linux实例内存溢出(OOM)问题修复方案
  • (四)机器学习小白入门YOLOv :图片标注实操手册
  • 深度学习环境配置:PyTorch、CUDA和Python版本选择
  • 工作中的思考
  • 推荐系统中的相似度
  • 【计算机网络】第三章:数据链路层(上)
  • Redis常用数据结构以及多并发场景下的使用分析:Sorted List类型
  • (电机03)分享FOC控制中SVPWM的输出关联硬件
  • 【6G新技术探索】A2A协议介绍
  • 数据出海的隐形冰山:企业如何避开跨境传输的“合规漩涡”?
  • BM10 两个链表的第一个公共结点
  • main(int argc,char **agrv)的含义
  • Kubernetes配置管理