其他分享
首页 > 其他分享> > 【Axum】返回 JSON 对象

【Axum】返回 JSON 对象

作者:互联网

环境

概念

参考:https://docs.rs/axum/latest/axum/index.html

示例

toml

[package]
edition = "2021"
name = "game"
version = "0.1.0"

[dependencies]
axum = "*"
serde = {version = "*", features = ["derive"]}
serde_json = "*"
tokio = {version = "*", features = ["full"]}

main.rs

use axum::{routing::get, Json, Router};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(home));

    let addr = SocketAddr::from(([127, 0, 0, 1], 4444));
    println!("listening on {addr}");
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

#[derive(Serialize, Deserialize)]
struct User {
    id: usize,
    name: String,
}

async fn home() -> Json<User> {
    Json(User {
        id: 4,
        name: "jiangbo".into(),
    })
}

总结

使用 axum 返回 JSON 对象。

附录

标签:返回,serde,Axum,use,Json,JSON,version,main,axum
来源: https://www.cnblogs.com/jiangbo44/p/16206388.html