forest HTTP调用API框架

Forest是一个高层的、极简的轻量级HTTP调用API框架。
相比于直接使用Httpclient不再用写一大堆重复的代码了,而是像调用本地方法一样去发送HTTP请求。

 

添加Maven依赖

<dependency>
    <groupId>com.dtflys.forest</groupId>
    <artifactId>forest-spring-boot-starter</artifactId>
    <version>1.5.11</version>
</dependency>

 

创建一个interface

以高德地图API为例 

复制代码
package com.xc.xcspringboot.client;

import com.dtflys.forest.annotation.Get;

import java.util.Map;

public interface AmapClient {

    /**
     * 聪明的你一定看出来了@Get注解代表该方法专做GET请求
     * 在url中的${0}代表引用第一个参数,${1}引用第二个参数
     */
    @Get("https://ditu.amap.com/service/regeo?longitude=${0}&latitude=${1}")
    Map getLocation(String longitude, String latitude);
}
复制代码

 

扫描接口

在Spring Boot的配置类或者启动类上加上@ForestScan注解,并在basePackages属性里填上远程接口的所在的包名

@SpringBootApplication
@ForestScan(basePackages = "com.xc.xcspringboot.client")
public class XcSpringbootApplication {

 

 

调用接口

复制代码
    // 注入接口实例
    @Resource
    private AmapClient amapClient;

    @RequestMapping(value = "/getLocation", method = RequestMethod.GET)
    public Object getLocation() {
        // 调用接口
        Map result = amapClient.getLocation("121.475078", "31.223577");
        log.info(result.toString());
        return result;
    }
复制代码

 

 

 

gitee:

https://gitee.com/dromara/forest

 

官方文档:

http://forest.dtflyx.com/docs/

posted @   草木物语  阅读(1053)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示