I am trying to learn rust with axum and planning to write a web server
Below is my server code
<code>#[tokio::main]
async fn main() {
dotenv().ok();
// Establish database connection
let db: DatabaseConnection = db::establish_connection().await;
let db = Arc::new(db);
// Build our application with a route
let app = Router::new()
.route("/admin/:adminname", get(get_admin))
.route("/user", post(create_user))
.layer(Extension(db.clone()));
// Run our app with hyper
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app)
.await
.unwrap();
}
</code>
<code>#[tokio::main]
async fn main() {
dotenv().ok();
// Establish database connection
let db: DatabaseConnection = db::establish_connection().await;
let db = Arc::new(db);
// Build our application with a route
let app = Router::new()
.route("/admin/:adminname", get(get_admin))
.route("/user", post(create_user))
.layer(Extension(db.clone()));
// Run our app with hyper
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app)
.await
.unwrap();
}
</code>
#[tokio::main]
async fn main() {
dotenv().ok();
// Establish database connection
let db: DatabaseConnection = db::establish_connection().await;
let db = Arc::new(db);
// Build our application with a route
let app = Router::new()
.route("/admin/:adminname", get(get_admin))
.route("/user", post(create_user))
.layer(Extension(db.clone()));
// Run our app with hyper
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app)
.await
.unwrap();
}
and my handler
<code>
#[derive(Deserialize)]
pub struct CreateUserRequest {
pub username: String,
pub password: String,
pub name: String,
pub age: i32,
pub gender: String,
pub birthday: String,
pub email: String,
pub phone: String,
pub address: String,
pub department_id: i32,
pub hire_date: String,
}
pub async fn create_user(
Json(payload): Json<CreateUserRequest>,
Extension(db): Extension<Arc<DatabaseConnection>>,
) -> impl IntoResponse {
let new_user = user_info::ActiveModel {
username: Set(payload.username),
password: Set(payload.password),
name: Set(payload.name),
age: Set(payload.age),
gender: Set(payload.gender),
birthday: Set(payload.birthday.parse().unwrap()), // Ensure proper parsing of date
email: Set(payload.email),
phone: Set(payload.phone),
address: Set(payload.address),
department_id: Set(payload.department_id),
hire_date: Set(payload.hire_date.parse().unwrap()), // Ensure proper parsing of date
..Default::default() // Remaining fields can be defaulted
};
match new_user.insert(&*db).await {
Ok(_) => StatusCode::CREATED,
Err(_) => StatusCode::INTERNAL_SERVER_ERROR
}
}
</code>
<code>
#[derive(Deserialize)]
pub struct CreateUserRequest {
pub username: String,
pub password: String,
pub name: String,
pub age: i32,
pub gender: String,
pub birthday: String,
pub email: String,
pub phone: String,
pub address: String,
pub department_id: i32,
pub hire_date: String,
}
pub async fn create_user(
Json(payload): Json<CreateUserRequest>,
Extension(db): Extension<Arc<DatabaseConnection>>,
) -> impl IntoResponse {
let new_user = user_info::ActiveModel {
username: Set(payload.username),
password: Set(payload.password),
name: Set(payload.name),
age: Set(payload.age),
gender: Set(payload.gender),
birthday: Set(payload.birthday.parse().unwrap()), // Ensure proper parsing of date
email: Set(payload.email),
phone: Set(payload.phone),
address: Set(payload.address),
department_id: Set(payload.department_id),
hire_date: Set(payload.hire_date.parse().unwrap()), // Ensure proper parsing of date
..Default::default() // Remaining fields can be defaulted
};
match new_user.insert(&*db).await {
Ok(_) => StatusCode::CREATED,
Err(_) => StatusCode::INTERNAL_SERVER_ERROR
}
}
</code>
#[derive(Deserialize)]
pub struct CreateUserRequest {
pub username: String,
pub password: String,
pub name: String,
pub age: i32,
pub gender: String,
pub birthday: String,
pub email: String,
pub phone: String,
pub address: String,
pub department_id: i32,
pub hire_date: String,
}
pub async fn create_user(
Json(payload): Json<CreateUserRequest>,
Extension(db): Extension<Arc<DatabaseConnection>>,
) -> impl IntoResponse {
let new_user = user_info::ActiveModel {
username: Set(payload.username),
password: Set(payload.password),
name: Set(payload.name),
age: Set(payload.age),
gender: Set(payload.gender),
birthday: Set(payload.birthday.parse().unwrap()), // Ensure proper parsing of date
email: Set(payload.email),
phone: Set(payload.phone),
address: Set(payload.address),
department_id: Set(payload.department_id),
hire_date: Set(payload.hire_date.parse().unwrap()), // Ensure proper parsing of date
..Default::default() // Remaining fields can be defaulted
};
match new_user.insert(&*db).await {
Ok(_) => StatusCode::CREATED,
Err(_) => StatusCode::INTERNAL_SERVER_ERROR
}
}
However, I encountered the following problem:
<code>error[E0277]: the trait bound `fn(axum::Json<software_final::handler::CreateUserRequest>, axum::Extension<std::sync::Arc<sea_orm::DatabaseConnection>>) -> impl std::future::Future<Output = impl axum::response::IntoResponse> {software_final::handler::create_user}: axum::handler::Handler<_, _>` is not satisfied
--> srcmain.rs:21:30
|
21 | .route("/user", post(create_user))
| ---- ^^^^^^^^^^^ the trait `axum::handler::Handler<_, _>` is not implemented for fn item `fn(Json<CreateUserRequest>, Extension<Arc<DatabaseConnection>>) -> impl Future<Output = ...> {create_user}`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `axum::handler::Handler<T, S>`:
<axum::handler::Layered<L, H, T, S> as axum::handler::Handler<T, S>>
<axum::routing::MethodRouter<S> as axum::handler::Handler<(), S>>
note: required by a bound in `axum::routing::post`
--> .cargoregistrysrcmirrors.ustc.edu.cn-61ef6e0cd06fb9b8axum-0.7.5srcroutingmethod_routing.rs:389:1
|
389 | top_level_handler_fn!(post, POST);
| ^^^^^^^^^^^^^^^^^^^^^^----^^^^^^^
| | |
| | required by a bound in this function
| 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)
</code>
<code>error[E0277]: the trait bound `fn(axum::Json<software_final::handler::CreateUserRequest>, axum::Extension<std::sync::Arc<sea_orm::DatabaseConnection>>) -> impl std::future::Future<Output = impl axum::response::IntoResponse> {software_final::handler::create_user}: axum::handler::Handler<_, _>` is not satisfied
--> srcmain.rs:21:30
|
21 | .route("/user", post(create_user))
| ---- ^^^^^^^^^^^ the trait `axum::handler::Handler<_, _>` is not implemented for fn item `fn(Json<CreateUserRequest>, Extension<Arc<DatabaseConnection>>) -> impl Future<Output = ...> {create_user}`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `axum::handler::Handler<T, S>`:
<axum::handler::Layered<L, H, T, S> as axum::handler::Handler<T, S>>
<axum::routing::MethodRouter<S> as axum::handler::Handler<(), S>>
note: required by a bound in `axum::routing::post`
--> .cargoregistrysrcmirrors.ustc.edu.cn-61ef6e0cd06fb9b8axum-0.7.5srcroutingmethod_routing.rs:389:1
|
389 | top_level_handler_fn!(post, POST);
| ^^^^^^^^^^^^^^^^^^^^^^----^^^^^^^
| | |
| | required by a bound in this function
| 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)
</code>
error[E0277]: the trait bound `fn(axum::Json<software_final::handler::CreateUserRequest>, axum::Extension<std::sync::Arc<sea_orm::DatabaseConnection>>) -> impl std::future::Future<Output = impl axum::response::IntoResponse> {software_final::handler::create_user}: axum::handler::Handler<_, _>` is not satisfied
--> srcmain.rs:21:30
|
21 | .route("/user", post(create_user))
| ---- ^^^^^^^^^^^ the trait `axum::handler::Handler<_, _>` is not implemented for fn item `fn(Json<CreateUserRequest>, Extension<Arc<DatabaseConnection>>) -> impl Future<Output = ...> {create_user}`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `axum::handler::Handler<T, S>`:
<axum::handler::Layered<L, H, T, S> as axum::handler::Handler<T, S>>
<axum::routing::MethodRouter<S> as axum::handler::Handler<(), S>>
note: required by a bound in `axum::routing::post`
--> .cargoregistrysrcmirrors.ustc.edu.cn-61ef6e0cd06fb9b8axum-0.7.5srcroutingmethod_routing.rs:389:1
|
389 | top_level_handler_fn!(post, POST);
| ^^^^^^^^^^^^^^^^^^^^^^----^^^^^^^
| | |
| | required by a bound in this function
| 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)
the dependencies are
<code>sea-orm = { version = "0.12", features = [ "sqlx-mysql", "runtime-tokio-rustls", "macros" ] }
axum = "0.7.5"
tokio = { version = "1.29.1", features = ["full"] }
tower = "0.4.13"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
dotenv = "0.15"
</code>
<code>sea-orm = { version = "0.12", features = [ "sqlx-mysql", "runtime-tokio-rustls", "macros" ] }
axum = "0.7.5"
tokio = { version = "1.29.1", features = ["full"] }
tower = "0.4.13"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
dotenv = "0.15"
</code>
sea-orm = { version = "0.12", features = [ "sqlx-mysql", "runtime-tokio-rustls", "macros" ] }
axum = "0.7.5"
tokio = { version = "1.29.1", features = ["full"] }
tower = "0.4.13"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
dotenv = "0.15"
it’s my first time using axum and i don’t really understand the info that compiler says
really want to know how to fix it and the reason why i got this problem
New contributor
abal_avalon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.