Skip to main content

请求处理器

请求处理器是一个异步函数,它接受零个或多个可以从请求中提取的参数(即 impl FromRequest),并返回一个可以转换为 HttpResponse 的类型(即 impl Responder)。

请求处理分为两个阶段。首先调用处理器对象,返回任何实现 Responder 特性的对象。然后,对返回的对象调用 respond_to(),将其转换为 HttpResponseError

默认情况下,Actix Web 为一些标准类型提供了 Responder 实现,例如 &'static strString 等。

有关实现的完整列表,请查看 Responder 文档

有效处理器的示例:

async fn index(_req: HttpRequest) -> &'static str {
"Hello world!"
}
async fn index(_req: HttpRequest) -> String {
"Hello world!".to_owned()
}

你也可以将签名更改为返回 impl Responder,这在涉及更复杂类型时效果很好。

async fn index(_req: HttpRequest) -> impl Responder {
web::Bytes::from_static(b"Hello world!")
}
async fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
...
}

使用自定义类型响应

要直接从处理器函数返回自定义类型,该类型需要实现 Responder 特性。

让我们为一个序列化为 application/json 响应的自定义类型创建一个响应:


流式响应体

响应体可以异步生成。在这种情况下,响应体必须实现流特性 Stream<Item = Result<Bytes, Error>>,即:


不同的返回类型(Either)

有时,你需要返回不同类型的响应。例如,你可以进行错误检查并返回错误,返回异步响应,或任何需要两种不同类型结果的情况。

对于这种情况,可以使用 Either 类型。Either 允许将两种不同的响应类型组合成一种类型。