如何在Rust Rocket中返回JSON响应并进行自动字段反序列化?

4

我正在尝试在 Rust 中创建一个打印服务器,当尝试发送 JSON 作为响应时遇到问题。

我在 Rocket 文档中发现,发送 JSON 作为响应非常容易:只需要使用 Serde 库即可。

不幸的是,对我来说这并不简单......

以下是我的当前代码:

#[derive(Serialize,Deserialize)]
pub struct Printers {
    pub printers: Vec<Printer>,
}

#[derive(Serialize,Deserialize)]
pub struct Printer {
    pub device_type: String,
    pub uid: String,
    pub provider: String,
    pub name: String,
    pub connection: String,
    pub version: u8,
    pub manufacturer: String,
}

/**
 * Here is my main problem 
 *   -> I want to send back the Printers Object as a JSON
 */
#[get("/printers")]
fn printers(key: ApiKey<'_>) -> Json<Printers> {
    let resp = crate::get_printers::get();
    Json(resp)
}

以下是错误代码:

   --> src/order_route.rs:25:33
    |
25  | fn printers(key: ApiKey<'_>) -> Json<Printers> {
    |                                 ^^^^^^^^^^^^^^ the trait `Responder<'_, '_>` is not implemented for `rocket_contrib::json::Json<order_route::Printers>`
    |
note: required by `route::handler::<impl Outcome<rocket::Response<'o>, Status, rocket::Data<'o>>>::from`
   --> ********/.cargo/registry/src/github.com-1ecc6299db9ec823/rocket-0.5.0-rc.1/src/route/handler.rs:188:5
    |
188 |     pub fn from<R: Responder<'r, 'o>>(req: &'r Request<'_>, responder: R) -> Outcome<'r> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

看起来我需要一个 特质(trait) 来完成这个,但是不知道如何创建它, 如果有人能帮我找到解决方案,我将非常感激。

[编辑]

这是 Cargo.toml 文件

[package]
name = "test"
version = "0.0.0"
edition = "2018"
[dependencies]
rocket = "0.5.0-rc.1"
reqwest = { version = "0.11", features = ["blocking", "json"] }
openssl = { version = "0.10", features = ["vendored"] }
json = "0.12.4"
serde = "1.0.127"
serde_json = "1.0.66"

[dependencies.rocket_contrib]
version = "0.4.10"
default-features = false
features = ["json"]

[workspace]
members = [
    ""
]

以下是完整的堆栈跟踪信息:

*****:~/*****/label-printer-server-rust/server$ cargo run
   Compiling hello v0.0.0 (/home/*****/*****/label-printer-server-rust/server)
error[E0277]: the trait bound `rocket_contrib::json::Json<order_route::Printers>: Responder<'_, '_>` is not satisfied
   --> src/order_route.rs:25:33
    |
25  | fn printers(key: ApiKey<'_>) -> Json<Printers> {
    |                                 ^^^^^^^^^^^^^^ the trait `Responder<'_, '_>` is not implemented for `rocket_contrib::json::Json<order_route::Printers>`
    |
note: required by `route::handler::<impl Outcome<rocket::Response<'o>, Status, rocket::Data<'o>>>::from`
   --> /home/*****/.cargo/registry/src/github.com-1ecc6299db9ec823/rocket-0.5.0-rc.1/src/route/handler.rs:188:5
    |
188 |     pub fn from<R: Responder<'r, 'o>>(req: &'r Request<'_>, responder: R) -> Outcome<'r> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
  --> src/order_route.rs:27:10
   |
27 |     Json(resp)
   |          ^^^^ expected struct `order_route::Printers`, found enum `Result`
   |
   = note: expected struct `order_route::Printers`
                found enum `Result<structures::Printers, Box<dyn StdError>>`

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `hello` due to 2 previous errors
3个回答

6

如果您发现有难度,这里是解决方案:

  • Enable JSON feature for rocket rust in your Cargo.toml file

    [package]
    #...
    
    [dependencies.rocket]
    version = "0.5.0-rc.1"
    features = ["json"]
    
    [dependencies.serde]
    version = "1.0.136"
    features = ["derive"]
    
    [dependencies]
    #...
    
    # rocket_contrib not required anymore
    
  • In your src/main.rs file

    // rocket v0.5.0-rc.1
    use rocket::{
       self,
       serde::{json::Json, Deserialize, Serialize},
    };
    
    #[derive(Deserialize, Serialize)]
    struct User {
       name: String,
       age: u8,
    }
    
    #[rocket::post("/post", format = "json", data = "<user>")]
    fn post_data(user: Json<User>) -> Json<User> {
       let name: String = user.name.clone();
       let age: u8 = user.age.clone();
    
       Json(User { name, age })
    }
    
    #[rocket::main]
    async fn main() {
       if let Err(err) = rocket::build()
          .mount("/", rocket::routes![post_data])
          .launch()
          .await
       {
          println!("Rocket Rust couldn't take off successfully!");
          drop(err); // Drop initiates Rocket-formatted panic
       }
    }
    
  • Test API using cURL

    curl -d '{"age": 12,"name":"John Doe"}' -H 'Content-Type: application/json' 
    http://localhost:8000/post
    
    # Response
    # {"name":"John Doe","age":12}
    

6
您正在使用 rocket 0.5.0-rc.1 和 rocket_contrib 0.4.10。虽然 rocket_contrib 中的 Json 实现了Responder, 但它实现的是 Rocket v4 的 Responder 特性,而不是 Rocket v5 的。
在 Rocket v5 中,Json 不再是 rocket_contib 的一部分,而是包含在 rocket crate 中(请注意,您需要在 rocket crate 上启用 json 特性):
use rocket::serde::json::Json;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct Printers {
    pub printers: Vec<Printer>,
}

#[derive(Serialize, Deserialize)]
pub struct Printer {
    pub device_type: String,
    pub uid: String,
    pub provider: String,
    pub name: String,
    pub connection: String,
    pub version: u8,
    pub manufacturer: String,
}

#[get("/printers")]
fn printers() -> Json<Printers> {
    todo!();
}

请注意,您现在可以从Cargo.toml中删除rocket_contrib(因为您没有使用其中的任何功能)。

感谢您的反馈! 经过检查,我无法找到箱子 rocket::serde::json::Json。 也许我需要更新我的 Cargo.toml 文件? - DirBear
你是否启用了 rocketjson 功能? - Elias Holzmann
1
我只找到了如何在rocket_contrib中实现这一点的方法。请问您有解释此点的文档链接吗? - DirBear
它必须看起来像这样。Cargo书还提供了有关如何在依赖项中启用功能以及一般情况下的Cargo.toml布局的更多信息。 - Elias Holzmann
2
再次感谢您的反馈,我在搜索时找到了Rocket文档中的规格说明,我在这里留下了链接,供那些遇到同样问题的人使用。JSON导入的Rocket文档链接!你让我的一天变得美好 :D - DirBear

0
我想补充一下Elias的答案,这些都在评论中提到了:
在你的cargo.toml文件中,你需要启用JSON功能:
[dependencies]
## SEE HERE!! 
rocket = {version ="0.5.0-rc.1", features=["json"]}

# other properties omitted for brevity 

# You can remove this!! Rocket Contrib doesn't exist in Rocket V0.5 
#[dependencies.rocket_contrib]
#version = "0.4.10"
#default-features = false
#features = ["json"]



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接