Forest v1.5.30 发布

发布时间 2023-04-16 11:18:22作者: LinuxProbe19
导读 Forest v1.5.30 发布,适配 SpringBoot3 和 Solon,新增延迟参数

Forest v1.5.30 发布Forest v1.5.30 发布

Forest 介绍

Forest 是一个开源的 Java HTTP 客户端框架,它能够将 HTTP 的所有请求信息(包括 URL、Header 以及 Body 等信息)绑定到您自定义的 Interface 方法上,能够通过调用本地接口方法的方式发送 HTTP 请求

获得奖项

2021 年度 OSC 中国开源项目评选「最受欢迎项目」

2022 年度 OSC 中国开源项目评选「最火热中国开源项目社区」

简单的栗子
声明式接口

创建一个 interface,并用 @Get 注解修饰接口方法。

public interface MyClient {
    @Get("http://localhost:8080/hello")
    String hello();
}

通过 @Get 注解,将上面的 MyClient 接口中的 simpleRequest() 方法绑定了一个 HTTP 请求, 其 URL 为 http://localhost:8080/hello,并默认使用 GET 方式,且将请求响应的数据以 String 的方式返回给调用者

编程式接口
Forest.get("http://localhost:8080/hello").execute();

编程式接口则更为简单直接

新增特性

支持和适配了 SpringBoot3

支持和适配了 Solon 框架

支持延迟参数特性

延迟参数 (Lambda 参数)

有很多情况,Header、Query、Body 的参数值不能马上得出,而是在请求发送前的那一刻(所有请求参数都到位时)才能得出,典型的例子就是加签验证的场景(在 Header 中添加一个参数 token,而 token 的值是对整个 body 做加密的结果)

请求头的延迟参数

Forest.post("/test")
        .addHeader("Content-Type", "application/json; charset=UTF-8")          // 普通请求头
        .addHeader("Authorization", req -> Base64.encode(req.body().encode())) // 延迟请求头
        .addBody("id", "1972664191")      // 请求体参数 id
        .addBody("name", "XieYu20011008") // 请求体参数 name
        .execute();

请求体的延迟参数

Forest.post("/test")
        .addHeader("Content-Type", "application/json; charset=UTF-8")  // 请求头
        .addHeader("_id", "20011008")                                  // 请求头
        .addBody("id", "1972664191")                                   // 请求体参数 id
        .addBody("name", req -> "Foo" + req.headerValue("_id"))        // 延迟请求体参数 name
        .addBody("token", req -> Base64.encode(req.body().encode()))   // 延迟请求体参数 token
        .execute();                                                    // 执行请求
官网和仓库地址

官网地址:
http://forest.dtflyx.com

Gitee 仓库地址:
https://gitee.com/dromara/forest

Github 仓库地址:
https://github.com/dromara/forest

本次更新内容
    • feat: 适配 springboot3
    • feat: 适配 solon
    • feat: 延迟参数 (Lambda 参数),支持 Query, Header,Body 三种参数的延迟求值
    • feat: 可自定义异步请求池拒绝策略
    • feat: 请求体序列化接口,ForestRequest.body ().encode () 和 ForestRequest.body ().encodeToString ()
    • fix: ForestProxy 添加 header 没有效果
    • fix: onBodyEncode 生命周期顺序问题
    • fix: 不同 ForestConfiguration 对象共用同一个异步线程池的问题
    • fix: 无法解析 localhost:8080 这类省略 http:// 的 url
    • reflector: 将 xml 解析模块拆分成了 forest-jaxb 和 forest-jakarta-xml 两个子模块,需要的情况要分别自行引入
    • reflector: request body encoder
    • refactor: Forest Body clone
    • refactor: 构建 Query String 部分
    • add: HTTPRoxy 注解的 headers 属性
    • add: forest 示例工程
www.linuxprobe.com