我得到一个;否';Json';“根目录中”;构建rust rocket api时出错
因此,我尝试按照中的示例构建一个简单的RESTAPI。中途,rust编译器给出了以下错误:我得到一个;否';Json';“根目录中”;构建rust rocket api时出错,rust,serde,rust-rocket,Rust,Serde,Rust Rocket,因此,我尝试按照中的示例构建一个简单的RESTAPI。中途,rust编译器给出了以下错误: 根目录中未解析的导入'rocket_contrib::Json'、'rocket_contrib::Value'无'Json' 我似乎不知道我做错了什么 这是我的货物。汤姆: [package] name = "rust-api-test" version = "0.1.0" authors = ["username"] edition = "2018" # See more keys and thei
根目录中未解析的导入'rocket_contrib::Json'、'rocket_contrib::Value'无'Json'
我似乎不知道我做错了什么
这是我的货物。汤姆:
[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
}
我在第10行得到错误:
使用rocket_contrib::{Json,Value}代码>
我跟着,现在它工作了。我将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();
}
#![功能(过程宏、数据宏)]
#[macro_use]外部板条箱火箭;
#[宏使用]外部板条箱火箭控制器;
#[宏使用]外部板条箱服务器;
现代英雄;
使用hero::{hero};
使用rocket_contrib::json::{json,JsonValue};
#[发布(“/”,数据=”)]
fn create(hero:Json)->Json{
英雄
}
#[获取(“/”)]
fn read()->JsonValue{
json([
“英雄1”,
“英雄2”
])
}
#[put(“/”,data=“”)]
fn更新(id:i32,hero:Json)->Json{
英雄
}
#[删除(“/”)]
fn delete(id:i32)->JsonValue{
json!({“状态”:“ok”})
}
#[获取(“/”)]
fn index()->&'static str{
“你好,世界!”
}
fn main(){
火箭:点火
.mount(“/”,routes![索引])
.mount(“/hero”,路径![创建、更新、删除])
.mount(“/英雄”,路线!)
.launch();
}
示例代码是为现已过时的Rocket 0.3.x版本编写的。您不能再基于旧版本启动新项目,因为某些依赖项已从crates.io中删除。但是,修复示例代码相对容易,因为编译器抱怨的导入没有被使用,所以您可以简单地删除它。在rocket\u contrib
的0.4.x版中,rocket\u contrib::Json
结构已移动到rocket\u contrib::Json::Json
,因此,如果需要,您还可以从新位置导入。rocket\u contrib::Value
enum已被替换为rocket\u contrib::json::JsonValue
,尽管使用了不同的实现,因此您可能需要根据新接口调整任何用途。示例代码是为现已过时的rocket 0.3.x版本编写的。您不能再基于旧版本启动新项目,因为某些依赖项已从crates.io中删除。但是,修复示例代码相对容易,因为编译器抱怨的导入没有被使用,所以您可以简单地删除它。在rocket\u contrib
的0.4.x版中,rocket\u contrib::Json
结构已移动到rocket\u contrib::Json::Json
,因此,如果需要,您还可以从新位置导入。rocket\u contrib::Value
enum已被替换为rocket\u contrib::json::JsonValue
,尽管使用了不同的实现,因此您可能需要根据新接口调整任何用途
#![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();
}