统一返回对象默认都是成功调用,那假如调用失败呢?code和message怎么设置?
来源:5-2 统一响应代码编写

qq_吼破_0
2020-05-14
老师你好,统一返回对象默认都是成功调用,那假如调用失败呢?code和message怎么设置?
// 定义最终的返回对象
CommonResponse<Object> response = new CommonResponse<>(
0, ""
);
// 如果 o 是 null, response 不需要设置 data
if (null == o) {
return response;
// 如果 o 已经是 CommonResponse, 不需要再次处理
} else if (o instanceof CommonResponse) {
response = (CommonResponse<Object>) o;
// 否则, 把响应对象作为 CommonResponse 的 data 部分
} else {
response.setData(o);
}
return response;
}
写回答
1回答
-
同学你好:
往后看,会讲到异常怎么处理,怎么返回
欢迎来 QQ 群随时交流、讨论,也非常感谢同学的支持!
212020-05-14
相似问题