为什么在``content'模块''中找不到Rocket的“ JSON”类型?
今天,我发现一个以前有效的项目不再编译。这是一个最小的示例,它重现了我的突然错误:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
内容
模块在Rocket Release候选物之间进行了更改。比较0.5.5.00.5.500.5.5.0 -rc.1
and0.5.0-rc.2
。JSON
类型移动到rought :: serde :: json :: json
。因此,您的代码应该看起来像这样:尽管您在不知不觉中似乎都在使用。如果
货物更新
运行,或者您根本没有保留cargo.lock.lock
文件(或重新创建的内容),则可能发生这种情况。如果rc.1
应将其视为与rc.2
兼容的话,这有点可疑版本通过将其在=
之前将其前缀(如果您想留在rc.1
:The
content
module was changed between Rocket release candidates. Compare the documentation for0.5.0-rc.1
and0.5.0-rc.2
. TheJson
type moved torocket::serde::json::Json
. So your code should look like this:It seems you did, albeit unknowingly. This can happen if
cargo update
was ran or you simply didn't preserve yourCargo.lock
file (or something recreated it). It is somewhat dubious ifrc.1
should be seen as compatible withrc.2
, but regardless, you can lock your dependency to a specific version by prefixing it with=
if you want to stay onrc.1
: