建设网站简单教程,电商行业,编程培训加盟,电脑赚钱的项目有哪些远程调用--Http Interface 前言1、导入依赖2、定义接口3 创建代理测试4、创建成配置变量 前言
这个功能是spring boot6提供的新功能#xff0c;spring允许我们通过自定义接口的方式#xff0c;给任意位置发送http请求#xff0c;实现远程调用#xff0c;可以用来简化… 远程调用--Http Interface 前言1、导入依赖2、定义接口3 创建代理测试4、创建成配置变量 前言
这个功能是spring boot6提供的新功能spring允许我们通过自定义接口的方式给任意位置发送http请求实现远程调用可以用来简化http远程访问需要webflux场景才可以
1、导入依赖 2、定义接口 package com.atguigu.boot305ssm.service;import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;/*** author jitwxs* date 2024年03月03日 20:39*/
public interface WeatherInterface {GetExchange(url/接口的后半截数据,accept application/json)String getWeather(RequestParam(area) String city,RequestHeader(Authorization) String auth);
}
3 创建代理测试 public MonoMonoString weather(String city){
// 创建客户端WebClient client WebClient.builder().baseUrl(要请求的接口).codecs(clientCodecConfigurer - {clientCodecConfigurer.defaultCodecs().maxInMemorySize(256*1024*1024);
// 响应数据两太大有可能会超过bufferSize,所以这里设置的大一些}).build();
// 创建工厂HttpServiceProxyFactory factory HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();// 获取代理对象WeatherInterface weatherAPI factory.createClient(WeatherInterface.class);MonoString weather weatherAPI.getWeather(西安,APPCODE 93b7e19861a24c519a7548b17dc46d75);return Mono.just(weather);}4、创建成配置变量