pub struct ResultBody<T> {
pub code: i32,
pub msg: String,
pub data: Option<T>,
}Expand description
API 统一响应体
所有 HTTP API 的响应均包装为此格式:
{
"code": 200,
"msg": "成功!",
"data": { ... }
}§构造方法
| 方法 | code | 用途 |
|---|---|---|
success() | 200 | 无数据成功响应 |
success_data() | 200 | 带数据成功响应 |
error() | 500 | 通用失败响应 |
error_code() | 自定义 | 指定错误码的失败响应 |
§Panics
本结构体及其方法均不 panic。序列化失败时通过 [IntoResponse]
优雅降级为 500 JSON 错误响应。
§辅助函数
permission_denied_response(): 权限不足 403download_forbidden(): 下载无权限 403download_failed(): 下载失败 500json_response(): 任意可序列化类型转 JSON 响应
Fields§
§code: i32响应码,200 成功
msg: String响应消息
data: Option<T>业务数据
Implementations§
Source§impl<T: Serialize> ResultBody<T>
impl<T: Serialize> ResultBody<T>
Sourcepub fn success() -> Self
pub fn success() -> Self
构建无数据的成功响应。
§返回值
返回 code=200, msg="成功!", data=None 的 ResultBody。
§示例
use tdm_server_rust::common::ResultBody;
let res: ResultBody<()> = ResultBody::success();
assert_eq!(res.code, 200);Sourcepub fn success_data(data: T) -> Self
pub fn success_data(data: T) -> Self
构建带数据的成功响应。
§返回值
返回 code=200, msg="成功!", data=Some(data) 的 ResultBody。
§示例
use tdm_server_rust::common::ResultBody;
let res = ResultBody::success_data(42);
assert_eq!(res.data, Some(42));Sourcepub fn error_code(code: i32, msg: impl Into<String>) -> ResultBody<()>
pub fn error_code(code: i32, msg: impl Into<String>) -> ResultBody<()>
构建指定错误码的失败响应。
§参数
code: 业务错误码,参见ErrorCode常量msg: 错误描述消息
§返回值
返回 code=<指定值>, msg=<传入消息>, data=None 的 ResultBody<()>。
Trait Implementations§
Source§impl<T: Clone> Clone for ResultBody<T>
impl<T: Clone> Clone for ResultBody<T>
Source§fn clone(&self) -> ResultBody<T>
fn clone(&self) -> ResultBody<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug> Debug for ResultBody<T>
impl<T: Debug> Debug for ResultBody<T>
Source§impl<T: Serialize> IntoResponse for ResultBody<T>
将 ResultBody 转为 axum HTTP 响应。
impl<T: Serialize> IntoResponse for ResultBody<T>
将 ResultBody 转为 axum HTTP 响应。
Content-Type 对齐 Java: application/json;charset=UTF-8。
§Errors
JSON 序列化失败时返回 500 {"code":500,"msg":"JSON 序列化失败: ...","data":null} 纯文本降级。
此错误不应在正常业务中出现,仅当 T 的 Serialize 实现有 bug 才会触发。
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
Auto Trait Implementations§
impl<T> Freeze for ResultBody<T>where
T: Freeze,
impl<T> RefUnwindSafe for ResultBody<T>where
T: RefUnwindSafe,
impl<T> Send for ResultBody<T>where
T: Send,
impl<T> Sync for ResultBody<T>where
T: Sync,
impl<T> Unpin for ResultBody<T>where
T: Unpin,
impl<T> UnsafeUnpin for ResultBody<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for ResultBody<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future
Call the handler with the given request.
§fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
Apply a [
tower::Layer] to the handler. Read more§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Convert the handler into a [
Service] by providing the state§impl<H, T> HandlerWithoutStateExt<T> for Hwhere
H: Handler<T, ()>,
impl<H, T> HandlerWithoutStateExt<T> for Hwhere
H: Handler<T, ()>,
§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Convert the handler into a [
Service] and no state.§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
Convert the handler into a
MakeService and no state. Read more§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
Convert the handler into a
MakeService which stores information
about the incoming connection and has no state. Read more§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request