远程调用--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、创建成配置变量


相关推荐
赵庆明老师6 小时前
Uniapp微信小程序开发:http请求封装。
http·微信小程序·uni-app
Y-Loong7 小时前
Kubernetes Pod 网络与 Service 网络全解析
网络·kubernetes
一只游鱼8 小时前
webSocket快速入门
网络·websocket·网络协议
努力的小郑10 小时前
有了TCP为什么还需要HTTP?再用RPC?这次彻底讲明白了
http·微服务·rpc
AALoveTouch11 小时前
大麦网抢票:基于Wireshark协议分析
网络·测试工具·wireshark
爱奥尼欧11 小时前
【Linux笔记】网络部分——socket 编程 TCP实现多台虚拟机使用指令访问云服务器
linux·服务器·网络
luopandeng11 小时前
amd npt技术 对比 intel ept 技术
java·linux·网络
迎風吹頭髮12 小时前
UNIX下C语言编程与实践60-UNIX TCP 套接字关闭:close 与 shutdown 函数的区别与使用场景
c语言·网络·unix
梁辰兴12 小时前
计算机操作系统:进程同步
网络·缓存·操作系统·进程·进程同步·计算机操作系统
hazy1k12 小时前
K230基础-录放视频
网络·人工智能·stm32·单片机·嵌入式硬件·音视频·k230