当前位置: 首页 > news >正文

Spring boot3-Http Interface: 声明式编程

来吧

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注解,再把我们创建的创建客户端给他,如果就一个接口文件就不用管

一健三连,下次一定

相关文章:

  • 第十九:channel 的使用
  • Docker容器安装软件(完整版)
  • 阿里云短信发送(工厂模式实现)
  • C++:二分习题
  • never_give_up
  • 【C++ 系列文章 基础 01 -- std::string 与 fmt::format】
  • Java线程安全
  • Vue3 深度解析:构建现代Web应用的全新范式
  • 【PCIe 总线及设备入门学习专栏 3 -- PCIe 三种路由方式详细介绍】
  • 淘晶驰 屏幕 应用 之 esp8266/arduino 简约时钟 2025/3/12
  • sql靶场-时间盲注(第九、十关)保姆级教程
  • Trae AI IDEA安装与使用
  • 【机器学习】主成分分析法(PCA)
  • 数组总和 (leetcode 40
  • MySql索引下推(ICP)是什么?有什么用?
  • logback希望特定的error日志写入到特定文件
  • Qt/C++音视频开发82-系统音量值获取和设置/音量大小/静音
  • leetcode_字符串 49. 字母异位词分组
  • DeepSeek赋能智慧环保:为环境资源保护提供决策支持,开启绿色智能新时代
  • MAVEN解决版本依赖冲突
  • 既是工具又是食物,可食用机器人开启舌尖上的新科技
  • 外企聊营商|武田制药:知识产权保护助创新药研发
  • 手机表面细菌菌落总数可能比马桶高10倍,医生详解如何洗手
  • 有人倒卖试运营门票?上海乐高乐园:这些票存在无法入园风险
  • 消息人士称泽连斯基已启程前往土耳其
  • 冰雹造成车损能赔吗?如何理赔?机构答疑