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


相关推荐
LlNingyu6 分钟前
简单流量分析,串通Wireshark基本使用
网络·wireshark·流量分析
茉莉玫瑰花茶11 分钟前
HTTPS 协议原理
网络协议·https·iphone
byoass31 分钟前
自动化任务系列之六:自动整理文件——按时间/关键字分类归档实战
运维·网络·安全·自动化·云计算
skilllite作者41 分钟前
OpenClaw 网络数据采集新手入门指南
网络
王大傻092842 分钟前
yakit 无法拦截127.0.0.0 数据包的解决方案
网络·安全
zhangzeyuaaa1 小时前
深入理解 Python GIL:从机制到释放时机
java·网络·python
枷锁—sha1 小时前
【CTFshow-pwn系列】03_栈溢出【pwn 072】详解:无字符串环境下的多级 Ret2Syscall 与 BSS 段注入
服务器·网络·汇编·笔记·安全·网络安全
tryqaaa_1 小时前
学习日志(二)【linux全部命令,http请求头{有例题},Php语法学习】
linux·学习·http·php·web
zhangzeyuaaa1 小时前
深入理解 Python 进程间通信:Queue 与 Pipe 实战解析
网络·python·中间件
桌面运维家2 小时前
Windows11 USB无线键鼠飘移故障排查与保养指南
网络