我在构建生锈火箭 api 时遇到 "no 'Json' in root" 错误
I get a "no 'Json' in root" error when building a rust rocket api
所以我试图按照 https://medium.com/sean3z/building-a-restful-crud-api-with-rust-1867308352d8 中的示例构建一个简单的 REST API。中途,rust 编译器给我以下错误:
unresolved imports 'rocket_contrib::Json', 'rocket_contrib::Value' no 'Json' in the root
我似乎无法弄清楚我做错了什么。
这是我的 Cargo.toml:
[package]
name = "rust-api-test"
version = "0.1.0"
authors = ["username"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.4"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
[dependencies.rocket_contrib]
version = "0.4.4"
default-features = false
features = ["json"]
hero.rs:
#[derive(Serialize, Deserialize)]
pub struct Hero {
pub id: Option<i32>,
pub name: String,
pub identity: String,
pub hometown: String,
pub age: i32
}
和main.rs:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate serde_derive;
mod hero;
use hero::{Hero};
use rocket_contrib::{Json, Value};
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
fn main() {
rocket::ignite().mount("/", routes![index]).launch();
}
我在第 10 行收到错误:
use rocket_contrib::{Json, Value};
我关注了 Sven Marnach's ,现在可以了。我将 main.rs 文件更改为以下内容:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate serde_derive;
mod hero;
use hero::{Hero};
use rocket_contrib::json::{Json, JsonValue};
#[post("/", data = "<hero>")]
fn create(hero: Json<Hero>) -> Json<Hero> {
hero
}
#[get("/")]
fn read() -> JsonValue {
json!([
"hero 1",
"hero 2"
])
}
#[put("/<id>", data = "<hero>")]
fn update(id: i32, hero: Json<Hero>) -> Json<Hero> {
hero
}
#[delete("/<id>")]
fn delete(id: i32) -> JsonValue {
json!({"status": "ok"})
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
fn main() {
rocket::ignite()
.mount("/", routes![index])
.mount("/hero", routes![create, update, delete])
.mount("/heroes", routes![read])
.launch();
}
示例代码是为现已过时的 0.3.x 版 Rocket 编写的。您不能再基于旧版本启动新项目,因为某些依赖项已从 crates.io 中删除。然而,修复示例代码相对容易——编译器抱怨的导入无论如何都没有使用,所以你可以简单地删除它。在 rocket_contrib
的版本 0.4.x 中,rocket_contrib::Json
结构已移至 rocket_contrib::json::Json
,因此如果需要,您也可以从新位置导入。 rocket_contrib::Value
枚举已替换为 rocket_contrib::json::JsonValue
,尽管实现方式不同,因此您可能需要调整任何用途以适应新接口。
所以我试图按照 https://medium.com/sean3z/building-a-restful-crud-api-with-rust-1867308352d8 中的示例构建一个简单的 REST API。中途,rust 编译器给我以下错误:
unresolved imports 'rocket_contrib::Json', 'rocket_contrib::Value' no 'Json' in the root
我似乎无法弄清楚我做错了什么。
这是我的 Cargo.toml:
[package]
name = "rust-api-test"
version = "0.1.0"
authors = ["username"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.4"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
[dependencies.rocket_contrib]
version = "0.4.4"
default-features = false
features = ["json"]
hero.rs:
#[derive(Serialize, Deserialize)]
pub struct Hero {
pub id: Option<i32>,
pub name: String,
pub identity: String,
pub hometown: String,
pub age: i32
}
和main.rs:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate serde_derive;
mod hero;
use hero::{Hero};
use rocket_contrib::{Json, Value};
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
fn main() {
rocket::ignite().mount("/", routes![index]).launch();
}
我在第 10 行收到错误:
use rocket_contrib::{Json, Value};
我关注了 Sven Marnach's
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate serde_derive;
mod hero;
use hero::{Hero};
use rocket_contrib::json::{Json, JsonValue};
#[post("/", data = "<hero>")]
fn create(hero: Json<Hero>) -> Json<Hero> {
hero
}
#[get("/")]
fn read() -> JsonValue {
json!([
"hero 1",
"hero 2"
])
}
#[put("/<id>", data = "<hero>")]
fn update(id: i32, hero: Json<Hero>) -> Json<Hero> {
hero
}
#[delete("/<id>")]
fn delete(id: i32) -> JsonValue {
json!({"status": "ok"})
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
fn main() {
rocket::ignite()
.mount("/", routes![index])
.mount("/hero", routes![create, update, delete])
.mount("/heroes", routes![read])
.launch();
}
示例代码是为现已过时的 0.3.x 版 Rocket 编写的。您不能再基于旧版本启动新项目,因为某些依赖项已从 crates.io 中删除。然而,修复示例代码相对容易——编译器抱怨的导入无论如何都没有使用,所以你可以简单地删除它。在 rocket_contrib
的版本 0.4.x 中,rocket_contrib::Json
结构已移至 rocket_contrib::json::Json
,因此如果需要,您也可以从新位置导入。 rocket_contrib::Value
枚举已替换为 rocket_contrib::json::JsonValue
,尽管实现方式不同,因此您可能需要调整任何用途以适应新接口。