Rocket: Trying to JSON response

Created on 20 Apr 2017  路  5Comments  路  Source: SergioBenitez/Rocket

![feature(plugin)]

![plugin(rocket_codegen)]

//extern crate rocket_contrib;
extern crate rocket;
extern crate mysql;
extern crate chrono;
extern crate serde_json;

[macro_use] extern crate rocket_contrib;

[macro_use] extern crate serde_derive;

use std::path::{Path, PathBuf};
use rocket_contrib::Template;
use rocket_contrib::{JSON, Value};
use rocket::response::NamedFile;

[dependencies]
rocket = "0.2.5"
rocket_codegen = "0.2.5"
mysql = "*"
chrono = "0.3"
serde = "0.9"
serde_json = "0.9"
serde_derive = "0.9"

error[E0432]: unresolved import rocket_contrib::JSON
--> src/main.rs:15:22
|
15 | use rocket_contrib::{JSON, Value};
| ^^^^ no JSON in the root

error[E0432]: unresolved import rocket_contrib::Value
--> src/main.rs:15:28
|
15 | use rocket_contrib::{JSON, Value};
| ^^^^^ no Value in the root

error: aborting due to 2 previous errors

invalid question

Most helpful comment

Chrono has a "serde" feature that needs to be enabled before you can serialize their types.

[dependencies]
chrono = { version = "0.3", features = ["serde"] }

All 5 comments

Did you enable the json feature?

I had added =D
I am trying to (Serialize, Deserialize) date: chrono::NaiveDateTime it shows some errors
the trait bound chrono::NaiveDateTime: model::event::_IMPL_DESERIALIZE_FOR_Event::_serde::Serialize is not satisfied

I have another problem

[error(404)]

fn not_found() -> Template {
let context = ();
Template::render("404", &context)
}

[get("/events", format = "application/json")]

fn events() -> JSON> {
let pool = helper::DB::connection();
let selected_events = model::Event::all(pool);
println!("Events {:?}", selected_events);
JSON(selected_events)
}

fn main() {
rocket::ignite()
.mount("/", routes![files, index, events])
.catch(errors![not_found])
.launch();
}

the route is exist
馃洶 Mounting '/':
=> GET / => GET /
=> GET /events application/json
when I put in url http://localhost:8000/events I get 404 error
GET /events:
=> Matched: GET / => Warning: Response was None.
=> Outcome: Failure
=> Warning: Responding with 404 Not Found catcher.
=> Response succeeded.

Chrono has a "serde" feature that needs to be enabled before you can serialize their types.

[dependencies]
chrono = { version = "0.3", features = ["serde"] }

Looks like this has been answered. If you're still having issues, please feel free to comment. Better yet, join us on #rocket at irc.mozilla.org or via the bridged Rocket room on Matrix.

cool! I have a problem with generate json the route events return 404 =?

[get("/events", format = "application/json")]

fn events() -> JSON> {
let pool = helper::DB::connection();
let selected_events = model::Event::all(pool);
println!("Events {:?}", selected_events);
JSON(selected_events)
}

fn main() {
rocket::ignite()
.mount("/", routes![files, index, events])
.catch(errors![not_found])
.launch();
}

GET /events:
=> Matched: GET / => Warning: Response was None.
=> Outcome: Failure
=> Warning: Responding with 404 Not Found catcher.
=> Response succeeded.

Was this page helpful?
0 / 5 - 0 ratings