Rust Axum POST method issue

I am trying to learn rust with axum and planning to write a web server

Below is my server code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật