在spring5中,新增加了响应式编程的内容。当我们想要使用http调用请求服务时,spring也提供了响应的服务调用的相关类WebClient
和HttpInterface
这两个远程调用http服务的类
使用WebClient
请求远程调用服务
// 创建webClient对象
WebClient webClient = WebClient.create();
// 发送请求
Map<String, String> hashMap = new HashMap<>();
hashMap.put("areaCn", "许昌");
Mono<String> mono = webClient
// 发送请求的方式
.get()
.uri("https://getweather.market.alicloudapi.com/lundear/weather1d?areaCn={areaCn}", hashMap)
// 响应的内容类型
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", "APPCODE b66c9fe78577400397065c061b3ba547")
.attribute("areaCn", city)
// 响应式
.retrieve()
// 转换为string类型的内容
.bodyToMono(String.class);
return mono;