为什么在``content'模块''中找不到Rocket的“ JSON”类型?

发布于 2025-01-28 22:01:01 字数 1355 浏览 3 评论 0原文

今天,我发现一个以前有效的项目不再编译。这是一个最小的示例,它重现了我的突然错误:

use rocket::{get, launch, routes};
use rocket::response::content;

#[get("/health")]
pub fn health() -> content::Json<String> {
    content::Json("ok".parse().unwrap())
}

#[launch]
async fn rocket() -> _ {
    rocket::build().mount("/actuator", routes![health])
}
error[E0412]: cannot find type `Json` in module `content`
 --> src\main.rs:5:29
  |
5 | pub fn health() -> content::Json<String> {
  |                             ^^^^ not found in `content`
  |
help: consider importing this struct
  |
1 | use rocket::serde::json::Json;
  |

error[E0425]: cannot find function, tuple struct or tuple variant `Json` in module `content`
 --> src\main.rs:6:14
  |
6 |     content::Json("ok".parse().unwrap())
  |              ^^^^ not found in `content`
  |
help: consider importing this tuple struct
  |
1 | use rocket::serde::json::Json;
  |

我正在使用货物1.59.0(49D8809DC 2022-02-10)rustc 1.59.0(9D1B2106E 20222-02-23)< /代码>。这是我的依赖性:

[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }

content :: JSON应该来自火箭,应由上面的货物功能启用。它肯定奏效了过去。我该怎么办来解决这个问题?这是怎么发生的?

Today I found that a project that previously worked no longer compiles. Here is a minimal example that reproduces my sudden error:

use rocket::{get, launch, routes};
use rocket::response::content;

#[get("/health")]
pub fn health() -> content::Json<String> {
    content::Json("ok".parse().unwrap())
}

#[launch]
async fn rocket() -> _ {
    rocket::build().mount("/actuator", routes![health])
}
error[E0412]: cannot find type `Json` in module `content`
 --> src\main.rs:5:29
  |
5 | pub fn health() -> content::Json<String> {
  |                             ^^^^ not found in `content`
  |
help: consider importing this struct
  |
1 | use rocket::serde::json::Json;
  |

error[E0425]: cannot find function, tuple struct or tuple variant `Json` in module `content`
 --> src\main.rs:6:14
  |
6 |     content::Json("ok".parse().unwrap())
  |              ^^^^ not found in `content`
  |
help: consider importing this tuple struct
  |
1 | use rocket::serde::json::Json;
  |

I am using cargo 1.59.0 (49d8809dc 2022-02-10) and rustc 1.59.0 (9d1b2106e 2022-02-23). And here are my dependencies:

[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }

The content::Json is supposed to come from Rocket and should be enabled by the cargo feature above. It definitely worked the past. What should I do to fix this problem? How did this happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2025-02-04 22:01:01

内容模块在Rocket Release候选物之间进行了更改。比较 0.5.5.00.5.500.5.5.0 -rc.1 and 0.5.0-rc.2 JSON类型移动到 rought :: serde :: json :: json 。因此,您的代码应该看起来像这样:

use rocket::serde::json::Json;

pub fn health() -> Json<String> {
    Json("ok".parse().unwrap())
}

但是我没有升级火箭版本,我仍在使用0.5.0-rc.1

尽管您在不知不觉中似乎都在使用。如果货物更新运行,或者您根本没有保留cargo.lock.lock文件(或重新创建的内容),则可能发生这种情况。如果rc.1应将其视为与rc.2兼容的话,这有点可疑版本通过将其在=之前将其前缀(如果您想留在rc.1

[dependencies]
rocket = { version = "=0.5.0-rc.1", features = ["json"] }

The content module was changed between Rocket release candidates. Compare the documentation for 0.5.0-rc.1 and 0.5.0-rc.2. The Json type moved to rocket::serde::json::Json. So your code should look like this:

use rocket::serde::json::Json;

pub fn health() -> Json<String> {
    Json("ok".parse().unwrap())
}

But I did not upgrade the rocket version, I am still using 0.5.0-rc.1

It seems you did, albeit unknowingly. This can happen if cargo update was ran or you simply didn't preserve your Cargo.lock file (or something recreated it). It is somewhat dubious if rc.1 should be seen as compatible with rc.2, but regardless, you can lock your dependency to a specific version by prefixing it with = if you want to stay on rc.1:

[dependencies]
rocket = { version = "=0.5.0-rc.1", features = ["json"] }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文