例如,当用户访问
总结: | id(输入) | 状态码 | 内容类型 | 响应体 | | --- | --- | --- | --- | | 1 | 200 | application/json | {"name": "world"} | | 2 | 400 | text/plain | no such person |
http://127.0.0.1:8080/hello
时,如果查询参数id
为1,则返回一个纯文本响应。如果id
为2,则返回一个json结构。总结: | id(输入) | 状态码 | 内容类型 | 响应体 | | --- | --- | --- | --- | | 1 | 200 | application/json | {"name": "world"} | | 2 | 400 | text/plain | no such person |
struct HelloParam {
id: u16,
}
struct HelloResponse {
name: String,
}
async fn hello_get(Query(params): Query<HelloParam>) -> Response {
// how to implement it?
}
let router= Router::new().route("/hello", get(hello_get));
heterogeneous_handle()
的参数。如果我使用Query<HelloParam>作为参数,我无法编译它。 - progquesterQuery(params): Query<HelloParam>
参数意味着它将处理与HelloParam
成员匹配的name=value
对的请求。因此,如果一个请求带有查询字符串?id=1
,则会使用相应设置调用您的处理程序函数类型为HelloParam
的结构体params
。然后,您只需匹配query.id
的值而不是headers.get(ACCEPT)
的值即可。 - d2718fn(Query<HelloParam>) -> impl Future<Output = Response<http_body::combinators::box_body::UnsyncBoxBody<axum::body::Bytes, axum::Error>>> {heterogeneous_handle}: Handler<_, _>
未满足 特质Handler<T, ReqBody>
已实现为Layered<S, T>
rustcE0277 libtest.rs(206, 45):此调用引入的约束要求 method_routing.rs(141, 16):axum::routing::get
中的约束要求 - progquester} _ => StatusCode::BAD_REQUEST.into_response(), } } - progquester