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


相关推荐
sweet丶2 小时前
MQTT消息通道-基础篇
网络协议
yychen_java3 小时前
当算法成为武器:AI泛滥时代的多维危机透视与治理路径
网络·人工智能·ai
漫途科技3 小时前
精准盯防危房隐患,智守人居安全|MTB46-4-2A 4G数据采集终端专项应用方案
网络·安全
奥利奥夹心脆芙4 小时前
辅助排查 HTTP 接口代码报错,实操完整案例分享
http
Misnearch4 小时前
抓包Packet Capture
网络·抓包
zhangfeng11334 小时前
ps aux讲解,结合国家超算中心 hpc apptainer
linux·服务器·网络
吠品5 小时前
一次 Nginx 报错 unexpected end of file 的排查记录
网络协议·https·ssl
代码中介商5 小时前
TLS握手全解析:从1.2到1.3的加密演进
网络·网络协议·http
xlq223225 小时前
66.ip
网络·网络协议·tcp/ip
tudoSearcher5 小时前
手机、平板、电脑同时控制Claude Code / Codex ?:Paseo实战指南
网络·开源·开源软件·个人开发·ai编程