This was one that bit me recently. I am using axum to develop my backend for a project I a developing. It takes json input from a post request and automatically deserializes it to a desired struct. However, I was getting this bizarre error message that took me a good amount of time to figure out. It turns out I just forgot to derive Deserialize on the struct. Extremely unintuitive.
use std::net::SocketAddr;
use axum::{Router, response::IntoResponse, routing::post, Json};
use serde::Deserialize;
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/test", post(test));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
// #[derive(Deserialize)] Without this the error is caused
struct Test {}
async fn test(
Json(_): Json<Test>
) {}
The resulting error is:
error[E0277]: the trait bound `fn(Json<Test>) -> impl Future<Output = impl IntoResponse> {test}: Handler<_, _>` is not satisfied
--> src/main.rs:9:30
|
9 | .route("/test", post(test));
| ---- ^^^^ the trait `Handler<_, _>` is not implemented for `fn(Json<Test>) -> impl Future<Output = impl IntoResponse> {test}`
| |
| required by a bound introduced by this call
|
= help: the trait `Handler<T, ReqBody>` is implemented for `Layered<S, T>`
note: required by a bound in `post`
--> /home/ford/.cargo/registry/src/github.com-1ecc6299db9ec823/axum-0.5.11/src/routing/method_routing.rs:400:1
|
400 | top_level_handler_fn!(post, POST);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `post`
= note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0277`.
warning: `atest` (bin "atest") generated 1 warning
error: could not compile `atest` due to previous error; 1 warning emitted
This was one that bit me recently. I am using axum to develop my backend for a project I a developing. It takes json input from a post request and automatically deserializes it to a desired struct. However, I was getting this bizarre error message that took me a good amount of time to figure out. It turns out I just forgot to derive Deserialize on the struct. Extremely unintuitive.
The resulting error is: