特征"FromSegments<"_>"未为"rocket::form:<ContentsRequest>:Form"实现



当我在rust rocketrocket = { version = "=0.5.0-rc.2", features = ["json"] }中添加表单请求时:

#[get("/page/<path..>")]
fn get_page(path: Form<ContentsRequest>) {  }

编译器显示如下错误:

error[E0277]: the trait bound `rocket::form::Form<ContentsRequest>: FromSegments<'_>` is not satisfied
--> src/main.rs:21:19
|
21 | fn get_page(path: Form<ContentsRequest>) {  }
|                   ^^^^ the trait `FromSegments<'_>` is not implemented for `rocket::form::Form<ContentsRequest>`

为什么会发生这种情况?我该怎么做才能修好它?这是完整的main.rs代码:

#[macro_use] extern crate rocket;
use rocket::form::Form;
use rocket_okapi::{mount_endpoints_and_merged_docs, rapidoc::*, swagger_ui::*};
use rocket_okapi::settings::UrlObject;
use rocket::serde::Deserialize;
use rocket::serde::Serialize;

#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![get_page])
}

#[get("/page/<path..>")]
fn get_page(path: Form<ContentsRequest>) {  }

use rocket_okapi::okapi::schemars::JsonSchema;
use rocket_okapi::okapi::schemars;
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize, FromForm, JsonSchema)]
#[allow(non_snake_case)]
pub struct ContentsRequest {
/// The contents_type
contents_type: i32
}

,这是Cargo.toml的依赖关系:

[package]
name = "rust-demo"
version = "0.1.0"
edition = "2018"
[dependencies]
rocket = { version = "=0.5.0-rc.2", features = ["json"] }
okapi = { git = "https://github.com/GREsau/okapi.git"}
schemars = "0.7"
rocket_okapi = { git = "https://github.com/GREsau/okapi.git", features = ["swagger", "rapidoc"] }

serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
serde_derive = "1.0"
# database
diesel = { version = "1.4.7", features = ["postgres","serde_json"] }
dotenv = "0.15.0"
jsonwebtoken = "7"
chrono = "0.4"
config = "0.11"
ring = "0.16.20"
md5 = "0.7.0"
data-encoding = "2.3.2"

匹配尾随查询参数的语法是?<name..>之前。也不需要在Form<_>:

中换行。
#[get("/page?<path..>")]
fn get_page(path: ContentsRequest) {}

文档

最新更新