| Crates.io | tin-actix-api-resp |
| lib.rs | tin-actix-api-resp |
| version | 0.1.2 |
| created_at | 2025-10-13 00:25:24.678352+00 |
| updated_at | 2025-10-13 00:25:24.678352+00 |
| description | A library for handling API responses in Actix-web applications |
| homepage | https://github.com/JiabinTang/tin-actix-api-resp |
| repository | https://github.com/JiabinTang/tin-actix-api-resp |
| max_upload_size | |
| id | 1879864 |
| size | 100,284 |
基于 Actix-Web 的统一 API 响应封装库
tin-actix-api-resp 提供了统一的 API 响应结构体 ApiRes<T>,支持标准 HTTP 状态码、业务自定义 code、消息 message 及数据 data,便于前后端协作和接口规范化。
在你的 Cargo.toml 中添加依赖:
[dependencies]
tin-actix-api-resp = { git = "https://gitee.com/J_Tang/tin-actix-api-resp.git" }
use tin_actix_api_resp::{ApiRes, ApiResult};
use actix_web::{get, web, App, HttpServer, Responder};
#[get("/hello")]
async fn hello() -> ApiResult<String> {
Ok(ApiRes::Ok("Hello, world!".to_string()))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(hello))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
成功响应:
{
"code": 200,
"message": "Success",
"data": "Hello, world!"
}
错误响应:
{
"code": 404,
"message": "Not Found"
}
ApiRes<T>:统一响应类型,支持 Ok/OkWith/Custom 及所有常见 HTTP 错误类型ApiResult<T>:类型别名,等价于 Result<ApiRes<T>, ApiErr>ApiErr:类型别名,等价于 ApiRes<()>ApiRes::Ok(data) // 200 成功
ApiRes::OkWith(201, "Created".into(), data) // 200+自定义 code/message
ApiRes::NotFound("资源不存在".into()) // 404
ApiRes::Custom(400, 10001, "参数错误".into(), None) // 自定义
MIT