Available on crate feature
resputil only.Expand description
统一 JSON 接口响应 { "code": int, "data": any, "msg": string }。
code: 业务码,CODE_OK(200) 表示成功, 与 HTTP 状态解耦data: 业务数据; 序列化结果为null时 (如()、None) 归一化为空对象{}, 永不返回null; 列表用Vec表达, 空列表自然输出[]msg: 提示信息, 对外一律英文短语
开启 axum feature 后 Response 实现 IntoResponse, handler 可直接返回:
ⓘ
use rust_web_utils::resputil::{self, Response};
async fn get_user() -> Response<User> {
match load_user().await {
Some(u) => resputil::ok(u),
None => resputil::fail(40401, "user not found").erase(),
}
}不用 Axum 时调用 Response::into_http 得到 http::Response<Full<Bytes>>。
Structs§
- Response
- 统一 JSON 响应; 用
ok/ok_msg/fail/fail_status构造
Constants§
- CODE_OK
- 成功业务码
Functions§
- fail
- 业务失败响应 (HTTP 200, data 为空对象);
code为业务错误码 - fail_
status - 带 HTTP 状态码的失败响应 (data 为空对象), 用于 401 / 403 / 429 等需按 HTTP 状态拦截的场景; 非法或 1xx 状态码矫正为 500, 避免 1xx 语义破坏 JSON 响应体
- ok
- 成功响应 (HTTP 200, code 200, msg
success) - ok_msg
- 带自定义提示的成功响应