WebClient,HTTP Interface远程调用阿里云API

 HTTP Interface

  1. Spring 允许我们通过定义接口的方式,给任意位置发送 http 请求,实现远程调用,可以用来简化 HTTP 远程访问。需要webflux场景才可

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
  2. 定义接口

    public interface BingService {
    
        @GetExchange(url = "/search",accept="application/json")//请求的地址,接收json数据
        Mono<String> search(@RequestParam("area") String keyword,
                        @RequsetHeader("Authorization") String auth);//在service里@GetExchange表示我要发一个请求参数叫"area"
    //和controller上不一样.
    }
  3. 创建代理&测试

    @SpringBootTest
    class Boot05TaskApplicationTests {
    
        @Test
        public Mono<String> weather(String city) {
            //1、创建客户端
            WebClient client = WebClient.builder()
                    .baseUrl("https://cn.bing.com")//给哪发请求
                    .codecs(clientCodecConfigurer -> {
                        clientCodecConfigurer
                                .defaultCodecs()
                                .maxInMemorySize(256*1024*1024);
                                //响应数据量太大有可能会超出BufferSize,所以这里设置的大一点
                    })
                    .build();
            //2、创建工厂
            HttpServiceProxyFactory factory = HttpServiceProxyFactory
                    .builder(WebClientAdapter.forClient(client)).build();
            //3、获取代理对象
            BingService bingService = factory.createClient(BingService.class);//BingService.class接口名
    
    
            //4、测试调用
            Mono<String> search = bingService.search(city,"APPCODE XXXXXXXX");
            return weather;
    
        }
    
    }

  4. 生产模式----编写配置类config/WeatherConfiguration,@config

    @Bean
    WeatherInterface WeatherInterface(){
    //1、创建客户端
            WebClient client = WebClient.builder()
                    .baseUrl("https://cn.bing.com")//给哪发请求
                    .codecs(clientCodecConfigurer -> {
                        clientCodecConfigurer
                                .defaultCodecs()
                                .maxInMemorySize(256*1024*1024);
                                //响应数据量太大有可能会超出BufferSize,所以这里设置的大一点
                    })
                    .build();
            //2、创建工厂
            HttpServiceProxyFactory factory = HttpServiceProxyFactory
                    .builder(WebClientAdapter.forClient(client)).build();
            //3、获取代理对象
            WeatherInterface WeatherInterface = factory.createClient(WeatherInterface.class);//BingService.class接口名
    return WeatherInterface;
    }
  5. WeatherService

    @Autowired
    WeatherInterface WeatherInterface
    
    
    
    //4、测试调用
            Mono<String> weather = WeatherInterface.search(city,"APPCODE XXXXXXXX");
            return weather;
    

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值