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

学习建网站家装设计方案ppt案例

学习建网站,家装设计方案ppt案例,晚上国网app,网站两个域名远程调用 API/SDK的区别: 1.WebClient 1.1创建与调用 创建WebClient非常简单: webclientcreate0webclient.create(string baseurl) 还可以使用webclient.builder0 配置更多参数项: uribuilderfactory: 自定义uribuilderfactory, 定义base…

远程调用

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/wzjs/590698.html

相关文章:

  • 做外贸什么网站好金华公司做网站
  • 统一用户管理系统池州网站优化
  • 网站屏蔽ip网页设计好的网站
  • 网站建设和网站设计图片制作教程
  • win7 iis 添加网站河北地图
  • 江门找人做网站排名上海企业建站推荐
  • 网站备案和域名解析网站实时推送怎么做
  • 建设网站套餐仿牌网站流量
  • 网站开发 鲁山重庆网站开发培训机构
  • php网站开发套模板网站根目录多出一.php
  • php 网站开发案例教程阿里云服务器上传网站内容
  • 网站中文名要注册的吗wordpress 关闭缓存
  • windows和linux做网站如何使用阿里云建站
  • 湘潭网站建设 都来磐石网络五金东莞网站建设技术支持
  • 娄底市网站建设网站html地图导航代码
  • html5 移动网站网站建设方案书要怎么样写
  • 网站导航栏兼容性网站广告推广技巧分享
  • 个人邮箱登录注册长沙网站seo收费标准
  • 高校校园网站建设的要求关于建筑建设的网站
  • 做笔记的网站网站的效果图
  • 创网站软件建站报告2000字
  • 网站专业术语中seo意思是site 危险网站
  • 做网站的企业排名科技公司排名
  • 天猫网站设计教程太原建站模板厂家
  • 长春网站怎么推广树莓派做网站
  • 厦门同安区建设局网站品牌营销推广策划公司
  • 做网站42类商标怎么选小类乐居房产官方网站
  • 用什么软件做网站最好和田网站建设
  • 做推广网站有什么WordPress知更鸟lts
  • 西宁最好网站建设公司哪家好wordpress 问答模块