场景:
现在很多springboot项目都会从阿里云上调用API接口进行使用外部服务,所以这次我将使用阿里云上的快递服务API来实现这一案例
首先我们要在登录阿里云,找到云市场里面的API
然后搜索快递
然后找到一个免费的进行购买测试使用
买了后可以看到如图的AppCode,这个有大作用,等下可以用到
然后点到自己买的快递API服务里面,这里面有个调用地址,划重点了,等下也要使用到
1.WebClient
首先要了解的是WebClient,这个东西就是一个发请求的一个配置。主要的创建和配置如下
非阻塞、响应式HTTP客户端
创建与配置
发请求:
- 请求方式: GET\POST\DELETE\xxxx
- 请求路径: /xxx
- 请求参数:aa=bb&cc=dd&xxx
- 请求头: aa=bb,cc=ddd
- 请求体:
创建 WebClient 非常简单:
- WebClient.create()
- WebClient.create(String baseUrl)
还可以使用 WebClient.builder() 配置更多参数项:
- uriBuilderFactory: 自定义UriBuilderFactory ,定义 baseurl.
- defaultUriVariables: 默认 uri 变量.
- defaultHeader: 每个请求默认头.
- defaultCookie: 每个请求默认 cookie.
- defaultRequest: Consumer 自定义每个请求.
- filter: 过滤 client 发送的每个请求
- exchangeStrategies: HTTP 消息 reader/writer 自定义.
- clientConnector: HTTP client 库设置.
例如我创建的客户端:
//1.创建客户端
WebClient client = WebClient.builder()
.defaultHeader("Authorization","APPCODE "+appcode)
.codecs(clientCodecConfigurer -> {
clientCodecConfigurer
.defaultCodecs()
.maxInMemorySize(256*1024*1024);
//响应数据量太大有可能会超出BufferSize,所以这里设置大一点
})
.build();
2.HTTP Interface
Spring 允许我们通过定义接口的方式,给任意位置发送 http 请求,实现远程调用,可以用来简化 HTTP 远程访问。需要webflux场景才可以做到
2.1 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
2.2 定义接口
public interface ExpressInterface {
@GetExchange(url = "https://wuliu.market.alicloudapi.com/kdi",accept = "application/json")
Mono<String> getExpress(@RequestParam("no") String no);
}
其中如上的:
- url:就是最开始提到的调用地址
- accept是收到的格式
2.3 创建代理
然后再定义一个代理配置类,进行写API的相关配置
@Configuration
public class WeatherConfiguration {
@Bean
HttpServiceProxyFactory factory(@Value("${aliyun.appcode}") String appcode){
//1.创建客户端
WebClient client = WebClient.builder()
.defaultHeader("Authorization","APPCODE "+appcode)
.codecs(clientCodecConfigurer -> {
clientCodecConfigurer
.defaultCodecs()
.maxInMemorySize(256*1024*1024);
//响应数据量太大有可能会超出BufferSize,所以这里设置大一点
})
.build();
//2.创建工厂
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builder(WebClientAdapter.forClient(client)).build();
return factory;
}
//天气预报API接口
@Bean
WeatherInterface weatherInterface(HttpServiceProxyFactory factory){
//3.获取代理对象
WeatherInterface weatherAPI = factory.createClient(WeatherInterface.class);
return weatherAPI;
}
//邮件查询API接口
@Bean
ExpressInterface expressInterface(HttpServiceProxyFactory factory){
ExpressInterface client = factory.createClient(ExpressInterface.class);
return client;
}
}
里面的@Value(${aliyun.appcode})
是我把相关的code写在了application.properties
配置文件中
aliyun.appcode=你的AppCode
然后再定义一个Service
实现类
@Service
public class ExpressService {
@Autowired
ExpressInterface expressInterface;
public Mono<String> express(String no)
{
Mono<String> weather = expressInterface.getExpress(no);
return weather;
}
}
最后再写一个Controller
进行测试
@RestController
public class ExpressController {
@Autowired
ExpressService expressService;
@GetMapping("getExpress")
public Mono<String> getExpress(@RequestParam("no") String no){
Mono<String> express = expressService.express(no);
return express;
}
}
测试
最后根据快递单号进行查询,就可以查询到相关信息
结束语
今天学习了如何调用阿里云API,这个还是很方便的,webflux
也是springboot3的新特性,所以还是挺不错的,而且上述的方法,阿里云的所有API都是适用的哦!