我想要一个带有 Axum 的自定义响应类型,并将业务逻辑映射到 HTTP 响应,如下所示:
use axum::{
http::StatusCode,
response::{Html, IntoResponse},
Json,
};
use serde::Serialize;
// Response
pub enum Response<T = ()> {
Created,
NoContent,
JsonData(T),
HtmlData(T),
}
impl<T> IntoResponse for Response<T>
where
T: Serialize
{
fn into_response(self) -> axum::response::Response {
match self {
Self::Created => StatusCode::CREATED.into_response(),
Self::NoContent => StatusCode::OK.into_response(),
Self::JsonData(data) => (StatusCode::OK, Json(data)).into_response(),
Self::HtmlData(data) => (StatusCode::OK, Html(data)).into_response(),
}
}
}
然而,这并不管用。
no method named `into_response` found for tuple `(StatusCode, Html<T>)` in the current scope
method not found in `(StatusCode, Html<T>)
我想知道为什么不能像使用 Json 那样使用 Html。