🌠 响应对象
# 响应对象
响应对象是 Forest 中最核心组件之一,其类名为ForestResponse
其主要承担的作用是,将服务端返回的响应信息(包括响应头、响应体、请求异常信息)封装在该对象中
并提供直观的API对返回的数据进行读取
# 获取响应对象
如同《请求对象》一样,在 Forest 中有两种获取 ForestResponse 对象的方法:声明式接口方法返回响应对象和编程式接口创建响应对象。
# 声明式
public interface MyClient {
    /**
     * Get类型请求,url路径为 /test
     * <p>ForestResponse是带泛型参数的类型
     * <p>泛型参数代表返回的响应数据所期望转化成的类型
     * 
     * @return Forest响应对象
     */
    @Get("/test")
    ForestResponse<String> getForestResponse();
}
 1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
调用 getForestResponse() 方法后即可马上发送请求,并且获取该请求所返回的 Forest 响应对象
@Resource
MyClient myClient;
... ...
// 发送请求,并获取从服务端返回的响应数据对象
ForestResponse<String> response = myClient.getForestResponse();
// 获取响应结果数据
String result = response.getResult();
 1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 编程式
ForestResponse response = Forest.get("/test")
        .execute(ForestResponse.class);
// 获取响应结果数据
String result = response.getResult();
 1
2
3
4
2
3
4
帮助我们改善此文档  (opens new window)
  上次更新: 2025/06/24, 01:16:57