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


相关推荐
普普通通的南瓜9 分钟前
IP证书在关键信息基础设施安全防护中的实践与挑战
网络·数据库·网络协议·tcp/ip·安全·ssl
讨厌下雨的天空2 小时前
网络基础
网络·1024程序员节
0和1的舞者3 小时前
《网络编程核心概念与 UDP Socket 组件深度解析》
java·开发语言·网络·计算机网络·udp·socket
华普微HOPERF3 小时前
Matter协议,如何赋能智能家居构建跨生态的互操作网络?
网络·智能家居
YFLICKERH3 小时前
【加密协议】SSL/TLS 协议工作流程
网络协议·ssl/tls
6***94153 小时前
报错The default superclass, “jakarta.servlet.http.HttpServlet“(已经配置好tomcat)
http·servlet·tomcat
河南博为智能科技有限公司4 小时前
动环监控终端-守护变电站安全运行的智能核心
运维·服务器·网络·物联网
无心水4 小时前
【Python实战进阶】5、Python字符串终极指南:从基础到高性能处理的完整秘籍
开发语言·网络·python·字符串·unicode·python实战进阶·python工业化实战进阶
Top`5 小时前
两个服务之间传递的数据本质上是字节码(Byte Stream)
网络
dyxal5 小时前
非对称加密:彻底解决密钥分发难题的数字安全革命
服务器·网络·安全