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环境使用
    • 🏹 Springboot3环境安装
    • 📐 Springboot3环境配置
    • 🎯 Springboot3环境使用
    • 🏹 Spring环境安装
    • 📐 Spring环境配置
    • 🎯 Spring环境使用
    • 🏹 Solon环境安装
    • 📐 Solon环境配置
    • 🎯 Solon环境使用
    • 🏹 原生Java环境安装
    • 📐 原生Java环境配置
    • 🎯 原生Java环境使用
    • 🧬 编程式接口
  • 配置项

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

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

      • 请求API

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

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

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

      • 🥪 拦截器
      • 🍏 自定义注解
      • 🍇 组合注解
      • 🥑 自定义转换器
    • v1.5.30文档
    • 声明式接口
    公子骏
    2022-07-01
    目录

    🚑 异常处理

    发送HTTP请求不会总是成功的,总会有失败的情况。Forest提供多种异常处理的方法来处理请求失败的过程。

    # try-catch方式

    最常用的是直接用try-catch。Forest请求失败的时候通常会以抛异常的方式报告错误, 获取错误信息只需捕获ForestNetworkException异常类的对象,如示例代码所示:

    /**
     * try-catch方式:捕获ForestNetworkException异常类的对象
     */
    try {
        String result = myClient.send();
    } catch (ForestNetworkException ex) {
        int status = ex.getStatusCode(); // 获取请求响应状态码
        ForestResponse response = ex.getResponse(); // 获取Response对象
        String content = response.getContent(); // 获取请求的响应内容
        String resResult = response.getResult(); // 获取方法返回类型对应的最终数据结果
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    # 回调函数方式

    第二种方式是使用OnError回调函数,如示例代码所示:

    /**
     * 在请求接口中定义OnError回调函数类型参数
     */
    @Request(
            url = "http://localhost:8080/hello/user",
            headers = {"Accept:text/plain"},
            data = "username=${username}"
    )
    String send(@Var("username") String username, OnError onError);
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    调用的代码如下:

    // 在调用接口时,在Lambda中处理错误结果
    myClient.send("foo",  (ex, request, response) -> {
        int status = response.getStatusCode(); // 获取请求响应状态码
        String content = response.getContent(); // 获取请求的响应内容
        String result = response.getResult(); // 获取方法返回类型对应的最终数据结果
    });
    
    1
    2
    3
    4
    5
    6

    注意

    加上OnError回调函数后便不会再向上抛出异常,所有错误信息均通过OnError回调函数的参数获得。

    # ForestResponse

    第三种,用ForestResponse类作为请求方法的返回值类型,示例代码如下:

    /**
     * 用`ForestResponse`类作为请求方法的返回值类型, 其泛型参数代表实际返回数据的类型
     */
    @Request(
            url = "http://localhost:8080/hello/user",
            headers = {"Accept:text/plain"},
            data = "username=${username}"
    )
    ForestResponse<String> send(@Var("username") String username);
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    调用和处理的过程如下:

    ForestResponse<String> response = myClient.send("foo");
    // 用isError方法判断请求是否失败, 比如404, 500等情况
    if (response.isError()) {
        int status = response.getStatusCode(); // 获取请求响应状态码
        String content = response.getContent(); // 获取请求的响应内容
        String result = response.getResult(); // 获取方法返回类型对应的最终数据结果
    }
    
    1
    2
    3
    4
    5
    6
    7

    注意

    以ForestResponse类为返回值类型的方法也不会向上抛出异常,错误信息均通过ForestResponse对象获得。

    # 拦截器方式

    若要批量处理各种不同请求的异常情况,可以定义一个拦截器, 并在拦截器的onError方法中处理异常,示例代码如下:

    public class ErrorInterceptor implements Interceptor<String> {
    
        // ... ...
    
        @Override
        public void onError(ForestRuntimeException ex, ForestRequest request, ForestResponse response) {
            int status = response.getStatusCode(); // 获取请求响应状态码
            String content = response.getContent(); // 获取请求的响应内容
            Object result = response.getResult(); // 获取方法返回类型对应的返回数据结果
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    提示

    关于具体如何使用拦截器请参见 《拦截器》

    帮助我们改善此文档 (opens new window)
    上次更新: 2023/03/13, 15:28:40
    🍉 上传下载
    🚀 请求对象

    ← 🍉 上传下载 🚀 请求对象→

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