Forest Forest
💒 首页
  • v1.5.30
  • v1.5.28
  • 🎄 ForestX
🌰 案例
💖 支持
🛫 更新记录
🧢 开发团队
⚒️ 参与贡献
  • MaxKey - 业界领先的身份管理和认证产品 (opens new window)
  • Snowy - 国内首个国密前后端分离快速开发平台 (opens new window)
  • Eoapi - 一个开源、可拓展的 API 工具平台 (opens new window)
  • Gitee (opens new window)
  • Github (opens new window)
💒 首页
  • v1.5.30
  • v1.5.28
  • 🎄 ForestX
🌰 案例
💖 支持
🛫 更新记录
🧢 开发团队
⚒️ 参与贡献
  • MaxKey - 业界领先的身份管理和认证产品 (opens new window)
  • Snowy - 国内首个国密前后端分离快速开发平台 (opens new window)
  • Eoapi - 一个开源、可拓展的 API 工具平台 (opens new window)
  • Gitee (opens new window)
  • Github (opens new window)
  • 序言

    • 🎁 新手介绍
    • 📖 文档
    • 🌰 使用案例
    • 🕵️‍ 关于作者
    • 👨‍🎓 贡献者列表
  • 入门

    • 🎬 安装配置说明
    • 🏹 Springboot环境安装
    • 📐 Springboot环境配置
    • 🎯 Springboot环境使用
    • 🏹 Spring环境安装
    • 📐 Spring环境配置
    • 🎯 Spring环境使用
    • 🏹 原生Java环境安装
    • 📐 原生Java环境配置
    • 🎯 原生Java环境使用
    • 🧬 编程式接口
  • 配置项

    • 👜 Springboot环境配置项
    • 👝 Spring环境配置项
    • 🎒 原生Java环境配置项
    • 📚 配置优先级/作用域
  • 声明式接口

    • 🧱 构建接口
    • 🍀 请求方法
    • 🚚 请求地址
    • 🎈 URL 参数
    • 🍭 请求头
    • 👔 请求体
    • 🍮 后端框架
    • 🧁 接口注解
    • 📬 接收数据
    • 🍛 数据转换
    • 🍓 成功/失败条件
    • 🍌 重试机制
    • 🥂 重定向
    • 🍔 Gzip解压
    • 🎂 日志管理
    • ⚽ 回调函数
    • 🍟 异步请求
    • 🛡️ HTTPS
    • 🍪 使用Cookie
    • 🛸 使用代理
    • 🍉 上传下载
    • 🚑 异常处理
  • 编程式接口

    • 请求API

      • 🚀 请求对象
      • 🚢 请求属性
      • ✨ 执行请求
        • 🎊 后端框架
        • 🎪 请求类型
        • 🔮 请求地址
        • 🧀 URL 参数
        • 🚅 请求头
        • 🚋 请求体
        • ⚓ 回调函数
        • 🚁 异步请求
        • 🥯 Cookie
        • 🍜 成功/失败条件
        • 🌶️ 重试机制
        • ⛵ 重定向
        • 🛰️ 请求代理
      • 响应API

        • 🌠 响应对象
        • ✒️ 读取数据
        • 🦋 响应状态码
        • 🏥 响应错误处理
        • 🎧 响应头
        • 🥞 Cookie
    • 模板表达式

      • 🍬 Hello World
      • 🍹 配置属性引用
      • 🍖 变量引用
      • 🥃 动态变量绑定
      • 🥗 参数序号引用
      • 🍍 引用对象属性
      • 🥝 调用对象方法
    • 高级特性

      • 🥪 拦截器
      • 🍏 自定义注解
      • 🍇 组合注解
      • 🥑 自定义转换器
    • v1.5.28文档
    • 编程式接口
    • 请求API
    公子骏
    2022-07-14
    目录

    ✨ 执行请求

    ForestRequest 对象可以直接调用 execute() 方法执行请求,即发送请求到远端服务器

    // 发送 Get 请求, url: http://localhost/
    Forest.get("/").execute();
    // execute 方法会返回服务端响应的数据
    Object ret = Forest.post("/").execute();
    
    1
    2
    3
    4

    Forest请求对象也提供多个方法,以满足用不同类型接受数据的需要

    # 自定义类型

    execute(Class<R> clazz) 执行请求发送过程

    • 参数clazz: 结果返回类型, Class 对象
    // 以字符串方式接受数据
    String str = Forest.get("/").execute(String.class);
    // 以自定义类型方式接受数据
    MyResult result = Forest.get("/").execute(MyResult.class);
    
    1
    2
    3
    4

    # Type类型

    execute(Type type) 执行请求发送过程

    • 参数type: 结果返回类型, Type 接口实例
    // 使用 TypeReference 工具类包裹泛型类型
    TypeReference<List<String>> typeRef = new TypeReference<List<String>>() {};
    // 获取 Type 对象
    Type type = typeRef.getType();
    // 以自定义Type类型形式接受数据
    List<String> strList = Forest.get("/").execute(type);
    
    1
    2
    3
    4
    5
    6

    注意

    new TypeReference实例化的时候一定要带上泛型参数,否则无效

    # 泛型类型

    execute(TypeReference typeReference) 执行请求发送过程

    • 参数typeReference: 结果返回类型的引用, Type 接口实例
    // 使用 TypeReference 工具类包裹泛型类型
    TypeReference<List<String>> typeRef = new TypeReference<List<String>>() {};
    // 以带复杂泛型参数的类型形式接受数据
    List<String> strList = Forest.get("/").execute(typeRef);
    
    1
    2
    3
    4

    注意

    • new TypeReference实例化的时候一定要带上泛型参数,否则无效
    • TypeReference是com.dtflys.forest.utils包下的类,不要引用到其它库中的同名类型

    # 字符串类型

    executeAsString() 执行请求发送过程,并获取字符串类型结果

    • 返回值 : 字符串类型数据
    // 直接以 String 类型接受数据
    String str = Forest.get("/").executeAsString();
    
    1
    2

    # 列表类型

    executeAsList() 执行请求发送过程,并获取List类型结果

    • 返回值 : 列表对象
    // 直接以 List 类型接受数据
    List<String> list = Forest.get("/").executeAsList();
    
    1
    2

    # Map类型

    executeAsMap() 执行请求发送过程,并获取Map类型结果

    • 返回值 : Map 对象
    // 直接以 Map 类型接受数据
    Map<String, String> map = Forest.get("/").executeAsMap();
    
    1
    2

    # 响应类型

    获取不带泛型的 Forest 响应对象

    // 直接以响应类型接受数据
    ForestResponse response = Forest.get("/").execute(ForestResponse.class);
    
    1
    2

    获取带泛型的 Forest 响应对象

    // 直接以响应类型接受数据, 并带有泛型参数类型
    ForestResponse<List<MyUser>> resposne = Forest.get("/").execute(
            new TypeReference<ForestResponse<List<MyUser>>>() {});
    
    1
    2
    3
    帮助我们改善此文档 (opens new window)
    上次更新: 2023/03/07, 12:59:48
    🚢 请求属性
    🎊 后端框架

    ← 🚢 请求属性 🎊 后端框架→

    Theme by Vdoing | Copyright © 2016-2023 公子骏 | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式