来吧
1.首先引入pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
2.创建WebClientController控制器
import com.atguigu.boot3_07_http.service.WebClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class WebClientController {
@Autowired
private WebClientService webClientService;//还是一样自动注解干活的程序猿webClientService类,稍后创建
/**
* 由于测试接的是阿里云天气api,需要带authorization头是固定的,所有放配置文件了
* HTTP Interface 推荐
* @param area
* @param authorization
* @return
*/
@GetMapping("/aliyahInterface/weather")
public Mono<String> aliyahweatherinterface(@RequestParam("area") String area,@Value("${aliyun.authorization}") String authorization) {//@Value()使用配置文件.yml或者.properties
if (area == null) {
return Mono.just("参数不能为空");
}// 返回错误信息
return webClientService.webWeather(area,authorization);
}
}
3.创建WebClientService类
import com.atguigu.boot3_07_http.interfaces.AliyahWeatherInterface; //接口文件
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
@Service
public class WebClientService {
@Autowired
AliyahWeatherInterface aliyahWeatherInterface; //接口文件
public Mono<String> webWeather(String area,String authorization) { //接收aliyahweatherinterface传过来的参数
return aliyahWeatherInterface.getWeather(area,authorization);//需要传的参数给到接口
}
}
4.创建AliyahWeatherInterface接口
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;
import reactor.core.publisher.Mono;
public interface AliyahWeatherInterface {
@GetExchange(url = "https://ali-weather.showapi.com/spot-to-weather",accept = "application/json") // url就是api地址,accept是接收的数据类型
Mono<String> getWeather(@RequestParam("area") String area, @RequestHeader("Authorization") String authorization); //这里的@RequestParam()是发送参数,@RequestHeader()设置请求头,按阿里云的规则来
}
5.创建WeatherlnerfaceConfig类
package com.atguigu.boot3_07_http.config;
import com.atguigu.boot3_07_http.interfaces.AliyahWeatherInterface;
import com.atguigu.boot3_07_http.interfaces.WeatherInterface;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
@Configuration
public class WeatherlnerfaceConfig {
@Bean
HttpServiceProxyFactory httpServiceProxyFactory(){
//1、创建客户端
WebClient client = WebClient.builder()
.codecs(clientCodecConfigurer -> {
clientCodecConfigurer
.defaultCodecs()
.maxInMemorySize(256 * 1024 * 1024);
//响应数据量太大有可能会超出BufferSize,所以这里设置的大一点
})
.build();
//2、spring boot3 3.4.*以上或者最新版本创建工厂用 WebClientAdapter.create(client)
// spring boot3 3.3.*已下的WebClientAdapter.forClient(client)
return HttpServiceProxyFactory
.builderFor(WebClientAdapter.create(client)).build();
}
//接口文件1,这里就是我们需要注入的创建好的aliyahWeatherInterface接口
@Bean
AliyahWeatherInterface aliyahWeatherInterface(HttpServiceProxyFactory httpServiceProxyFactory) {
//3、获取代理对象
return httpServiceProxyFactory.createClient(AliyahWeatherInterface.class);
}
//如果有多个接口文件
//接口文件2
@Bean
WeatherInterface weatherInterface(HttpServiceProxyFactory httpServiceProxyFactory) {
//3、获取代理对象
return httpServiceProxyFactory.createClient(WeatherInterface.class);
}
//接口文件3
............
}
我们看下请求阿里云天气api的结果
这样就可用了,梳理一下,接口怎么对接
1、WebClientService类自动注入接口文件
2、AliyahWeatherInterface接口文件@GetExchange接口地址参数啥啥的
其他的交给工厂是不是比上一篇的WebClient方便多了,官方推荐少量的用WebClient
项目大请求api多的用HTTP Interface
注意:如果有多个接口文件,记得到WeatherlnerfaceConfig类@Bean注解,再把我们创建的创建客户端给他,如果就一个接口文件就不用管
一健三连,下次一定