网页规划设计方案,云南网站建设快速优化,长安网站建设,中企动力官网邮箱HTTP Interface Spring 允许我们通过定义接口的方式#xff0c;给任意位置发送 http 请求#xff0c;实现远程调用#xff0c;可以用来简化 HTTP 远程访问。需要webflux场景才可 dependencygroupIdorg.springframework.boot/groupIdartifactId给任意位置发送 http 请求实现远程调用可以用来简化 HTTP 远程访问。需要webflux场景才可 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-webflux/artifactId
/dependency 定义接口 public interface BingService {GetExchange(url /search,acceptapplication/json)//请求的地址,接收json数据MonoString search(RequestParam(area) String keyword,RequsetHeader(Authorization) String auth);//在service里GetExchange表示我要发一个请求参数叫area
//和controller上不一样.
} 创建代理测试 SpringBootTest
class Boot05TaskApplicationTests {Testpublic MonoString weather(String city) {//1、创建客户端WebClient client WebClient.builder().baseUrl(https://cn.bing.com)//给哪发请求.codecs(clientCodecConfigurer - {clientCodecConfigurer.defaultCodecs().maxInMemorySize(256*1024*1024);//响应数据量太大有可能会超出BufferSize所以这里设置的大一点}).build();//2、创建工厂HttpServiceProxyFactory factory HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();//3、获取代理对象BingService bingService factory.createClient(BingService.class);//BingService.class接口名//4、测试调用MonoString search bingService.search(city,APPCODE XXXXXXXX);return weather;}} 生产模式----编写配置类config/WeatherConfiguration,config Bean
WeatherInterface WeatherInterface(){
//1、创建客户端WebClient client WebClient.builder().baseUrl(https://cn.bing.com)//给哪发请求.codecs(clientCodecConfigurer - {clientCodecConfigurer.defaultCodecs().maxInMemorySize(256*1024*1024);//响应数据量太大有可能会超出BufferSize所以这里设置的大一点}).build();//2、创建工厂HttpServiceProxyFactory factory HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();//3、获取代理对象WeatherInterface WeatherInterface factory.createClient(WeatherInterface.class);//BingService.class接口名
return WeatherInterface;
} WeatherService Autowired
WeatherInterface WeatherInterface//4、测试调用MonoString weather WeatherInterface.search(city,APPCODE XXXXXXXX);return weather;