Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何为Actix Web中的多个方法使用routes属性宏_Rust_Rust Actix_Actix Web - Fatal编程技术网

Rust 如何为Actix Web中的多个方法使用routes属性宏

Rust 如何为Actix Web中的多个方法使用routes属性宏,rust,rust-actix,actix-web,Rust,Rust Actix,Actix Web,在中,如何使用路由属性宏(#[http_method(“route”)])将多个http方法绑定到一个函数 例如,我有一个微不足道的端点: ///返回UUID4。 #[获取(“/uuid”)] 异步fn uuid_v4()->impl响应程序{ HttpResponse::Ok().json(Uuid{ uuid:uuid::uuid::new_v4(), }) } 我想拥有相同的端点句柄HEAD请求,我该怎么做? 我最初的方法是将宏堆叠起来: ///返回UUID4。 #[获取(“/uuid”

在中,如何使用路由属性宏(
#[http_method(“route”)]
)将多个http方法绑定到一个函数

例如,我有一个微不足道的端点:

///返回UUID4。
#[获取(“/uuid”)]
异步fn uuid_v4()->impl响应程序{
HttpResponse::Ok().json(Uuid{
uuid:uuid::uuid::new_v4(),
})
}
我想拥有相同的端点句柄
HEAD
请求,我该怎么做? 我最初的方法是将宏堆叠起来:

///返回UUID4。
#[获取(“/uuid”)]
#[标题(“/uuid”)]
异步fn uuid_v4()->impl响应程序{
HttpResponse::Ok().json(Uuid{
uuid:uuid::uuid::new_v4(),
})
}
但我确实遇到了一个编译错误:

    |
249 | async fn uuid_v4() -> impl Responder {
    |          ^^^^^^^ the trait `actix_web::handler::Factory<_, _, _>` is not implemented for `<uuid_v4 as actix_web::service::HttpServiceFactory>::register::uuid_v4`
|
249 |异步fn uuid_v4()->impl响应程序{
|^^^^^^特性“actix\u web::handler::Factory”未为“::register::uuid\u v4”实现`

我已经浏览了
actix web
actix web codegen文档
,但没有找到任何解决此问题的方法

我假设您正在使用
actix web:2.0.0
actix rt:1.0.0
以及您传递给
App.service
方法的此处理程序,如下所示

HttpServer::新建(移动| |{
App::new()
.wrap(中间件::记录器::默认值())
.服务(索引)
})
.bind(((“127.0.0.1”,自端口))?
.工人(8)
.run()
.等待
然后您需要像这样编写处理程序->

///返回UUID4。
#[获取(“/uuid”)]
异步fn uuid_v4(请求:HttpRequest)->结果{
让uuid_头=req
.headers()
.get(“uuid”)
.然后(| v | v.to_str().ok())
.unwrap|或|else(| |“某些id”);
//curl-H“uuid:username”localhost:8080
println!(“使用{}”,uuid_头);
Ok(web::Json(Uuid{
uuid:uuid::uuid::new_v4(),
}))
}
您可以这样做

#[route("/", method="GET", method="POST", method="PUT")]
async fn index() -> impl Responder {
  HttpResponse::Ok().body("Hello world!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
  HttpServer::new(move || {
    App::new()
        .service(index)
  })
  .bind("127.0.0.1:8080")?
  .run()
  .await
}
[route(“/”,method=“GET”,method=“POST”,method=“PUT”)] 异步fn index()->impl响应程序{ HttpResponse::Ok().body(“你好,世界!”) } #[actix_web::main] 异步fn main()->std::io::Result{ HttpServer::新建(移动| |{ App::new() .服务(索引) }) .bind(“127.0.0.1:8080”)? .run() .等待 }
一个资源具有多个路径和多个方法的示例

async fn index() -> impl Responder {
  HttpResponse::Ok().body("Hello world!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
  HttpServer::new(move || {
    App::new()
        .service(
            actix_web::web::resource(vec!["/", "/index"])
                .route(actix_web::web::get().to(index))
                .route(actix_web::web::post().to(index))
            )
  })
  .bind("127.0.0.1:8080")?
  .run()
  .await
}
async fn index()->impl响应程序{
HttpResponse::Ok().body(“你好,世界!”)
}
#[actix_web::main]
异步fn main()->std::io::Result{
HttpServer::新建(移动| |{
App::new()
.服务(
actix_web::web::resource(vec![“/”,“/index”])
.route(actix_web::web::get().to(index))
.route(actix_web::web::post().to(index))
)
})
.bind(“127.0.0.1:8080”)?
.run()
.等待
}

GET方法之外,我不明白该方法如何支持任何其他方法。因此,您会对试图帮助您的人投反对票。:)您的问题可能需要编辑,请尝试简单解释。我没有对您投反对票。我对答案投了反对票。问题是关于在函数上使用多个属性宏。例如
get
宏。