Springboot远程调用API(阿里云的API接口)

场景:
现在很多springboot项目都会从阿里云上调用API接口进行使用外部服务,所以这次我将使用阿里云上的快递服务API来实现这一案例

首先我们要在登录阿里云,找到云市场里面的API
image.png
然后搜索快递
image.png
然后找到一个免费的进行购买测试使用
image.png
买了后可以看到如图的AppCode,这个有大作用,等下可以用到
image.png
然后点到自己买的快递API服务里面,这里面有个调用地址,划重点了,等下也要使用到
image.png

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;
    }
}

测试

最后根据快递单号进行查询,就可以查询到相关信息
image.png

结束语

今天学习了如何调用阿里云API,这个还是很方便的,webflux也是springboot3的新特性,所以还是挺不错的,而且上述的方法,阿里云的所有API都是适用的哦!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值