远程调用--Http Interface

远程调用--Http Interface

前言

这个功能是spring boot6提供的新功能,spring允许我们通过自定义接口的方式,给任意位置发送http请求,实现远程调用,可以用来简化http远程访问,需要webflux场景才可以

1、导入依赖

2、定义接口

java 复制代码
package com.atguigu.boot305ssm.service;

import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;

/**
 * @author jitwxs
 * @date 2024年03月03日 20:39
 */
public interface WeatherInterface {

    @GetExchange(url="/接口的后半截数据",accept = "application/json")
    String getWeather(@RequestParam("area") String city,
                      @RequestHeader("Authorization") String auth);
}

3 创建代理&测试

java 复制代码
    public Mono<Mono<String>> weather(String city){
//        创建客户端
        WebClient client = WebClient.builder()
                .baseUrl("要请求的接口")
                .codecs(clientCodecConfigurer -> {
                    clientCodecConfigurer
                            .defaultCodecs()
                            .maxInMemorySize(256*1024*1024);
//                    响应数据两太大有可能会超过bufferSize,所以这里设置的大一些
                })
                .build();
//        创建工厂
        HttpServiceProxyFactory factory = HttpServiceProxyFactory
                .builder(WebClientAdapter.forClient(client)).build();

//        获取代理对象
        WeatherInterface weatherAPI = factory.createClient(WeatherInterface.class);

        Mono<String> weather = weatherAPI.getWeather("西安","APPCODE 93b7e19861a24c519a7548b17dc46d75");

        return Mono.just(weather);
    }

4、创建成配置变量


相关推荐
hbugs00116 分钟前
【案例分享】华为金融骨干网SRv6多层组网
网络·华为·金融·架构·eve-ng·bpf·流量洞察
如夏倾城1 小时前
NGFW ( Next-Generation Firewall,下一代防火墙)
运维·服务器·网络·http·网络安全·ssl·信息与通信
鲁邦通物联网2 小时前
AGV多控制器通信适配实操:IP规划、配置模板与出厂验证
网络·4g工业路由器·工业路由器·4g路由器·工业级路由器·5g工业路由器·出海联网
橘色的喵11 小时前
zmq Linux 内核与系统调优(ARM64 嵌入式平台)
linux·网络·性能优化·zmq·内核优化
国科安芯16 小时前
看不见的地基:ASP4644S2B四通道电源模块与星载电源分配网络的完整性设计
网络·嵌入式硬件·fpga开发·adc·电源模块·抗辐射·载荷
袁章根19 小时前
WiFi 外围电路设计(基于正基科技AP6275PR3模块)
网络·科技·硬件架构·硬件工程
老秦包你会1 天前
计算机网络第四课 ----Socket(套接字)编程TCP
网络协议·tcp/ip·计算机网络
charliejohn1 天前
计算机考研 408 网络 DNS 概念
运维·服务器·网络
羽落•无痕(苏晗默)1 天前
华为数通IA——16、数据中心网络基础:从通用计算到智能计算的网络演进
网络·计算机网络·网络安全·华为·信息与通信
优化Henry1 天前
5G NR RachPreambleFormat 参数:前导码格式、配置与参数用途
网络·5g·信息与通信·tdd