{"id":11744,"date":"2024-01-19T05:01:21","date_gmt":"2024-01-18T20:01:21","guid":{"rendered":"https:\/\/www.webcyou.com\/?p=11744"},"modified":"2024-01-19T05:01:22","modified_gmt":"2024-01-18T20:01:22","slug":"rust-actix-web-x-json-%e9%9d%99%e7%9a%84%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92api%e3%81%a7%e8%bf%94%e5%8d%b4","status":"publish","type":"post","link":"https:\/\/www.webcyou.com\/?p=11744","title":{"rendered":"Rust &#8211; Actix Web \u00d7 JSON \u9759\u7684\u30d5\u30a1\u30a4\u30eb\u3092API\u3067\u8fd4\u5374"},"content":{"rendered":"\n<p>\u3069\u3082\u3067\u3059\u3002<\/p>\n<p>\u524d\u56de\u300c<strong><a href=\"https:\/\/www.webcyou.com\/?p=11736\">Rust \u2013 Actix Web \u00d7 MongoDB\u74b0\u5883\u3092\u30b5\u30af\u30c3\u3068\u8d77\u52d5<\/a><\/strong>\u300d\u3068\u3044\u3046\u8a18\u4e8b\u3092\u66f8\u304d\u307e\u3057\u305f\u304c\u3001\u516c\u958b\u3055\u308c\u3066\u3044\u308b\u5909\u5316\u306e\u306a\u3044\u56fa\u5b9a\u306e\u30c7\u30fc\u30bf\u3001\u6240\u8b02\u30de\u30b9\u30bf\u30fc\u30c7\u30fc\u30bf\u306b\u95a2\u3057\u3066\u306f\u3001mongoDB\u306e\u65b9\u306b\u4fdd\u5b58\u305b\u305a\u306bJSON\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u30b5\u30fc\u30d0\u30fc\u4e0a\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3001\u305d\u3061\u3089\u3092\u8aad\u307f\u8fbc\u307fActix Web\u306eAPI\u3068\u3057\u3066\u8fd4\u5374\u306f\u3067\u304d\u306a\u3044\u304b\u3068\u8abf\u3079\u305f\u3068\u3053\u308d\u3001\u5bb9\u6613\u306b\u884c\u3048\u305f\u306e\u3067\u305d\u3061\u3089\u306e\u5099\u5fd8\u9332\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u30bd\u30fc\u30b9\u3092\u5148\u306b\u78ba\u8a8d\u3057\u305f\u3044\u65b9\u306f\u3053\u3061\u3089\u3092\u53c2\u7167\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/rust-game-samples\/actix_web_sample\/tree\/main\/static_json\">Actix Web Static JSON<\/a><\/p>\n<p><!-- notionvc: e9a53dda-651b-423f-bac2-66659291fa80 --><\/p>\n<h4>\u30b5\u30fc\u30d0\u30fc\u69cb\u7bc9<\/h4>\n<p>\u4eca\u56de\u306f\u6e96\u5099\u7b49\u306f\u5272\u611b\u3055\u305b\u3066\u9802\u304d\u3001Actix Web\u306e\u5b9f\u88c5\u3092\u884c\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u30bd\u30fc\u30b9\u81ea\u4f53\u3082\u9577\u304f\u306a\u3044\u306e\u3067\u3001main.rs\u4e00\u3064\u306b\u96c6\u7d04\u3057\u3066\u8a18\u8ff0\u3057\u307e\u3059\u3002\u307e\u305a\u306fUser\u306estruct\u306e\u4f5c\u6210\u3002<\/p>\n<p>\u3053\u3061\u3089\u306f\u524d\u56de\u3068\u5909\u308f\u308a\u306a\u3044\u5f62\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><!-- notionvc: 18cfa275-5cfc-4109-adf9-922a6164f911 --><\/p>\n<h5><span class=\"notion-enable-hover\" data-token-index=\"0\">\u69cb\u9020\u4f53\u306e\u5b9a\u7fa9<\/span><!-- notionvc: 1385b748-482a-44d3-8f1c-46a49136e699 --><\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\">use serde::{Deserialize, Serialize};\n\n#[derive(Serialize, Deserialize)]\nstruct User {\n    id: u32,\n    first_name: String,\n    last_name: String,\n    username: String,\n    email: String,\n}<\/pre>\n<p>JSON\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>JSON\u30c7\u30fc\u30bf\u306fUser\u3092\u8907\u6570\u6301\u3064\u5f62\u306a\u306e\u3067\u3001User\u306e\u914d\u5217\u3068\u3057\u3066\u4f5c\u6210\u3057\u3001user_list.json\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u3067data\u30d5\u30a9\u30eb\u30c0\u4ee5\u4e0b\u306b\u914d\u7f6e\u3057\u307e\u3059\u3002\uff08\u30e6\u30fc\u30b6\u30fc\u30c7\u30fc\u30bf\u306fchatGPT\u306b\u4f5c\u3063\u3066\u9802\u304d\u307e\u3057\u305f\u3002\uff09<\/p>\n<p>\u4eca\u56de\u306f\u30c7\u30e2\u306a\u306e\u3067\u3001\u30e6\u30fc\u30b6\u30fc\u30c7\u30fc\u30bf\u3068\u3057\u3066\u304a\u308a\u307e\u3059\u304c\u3001\u30e6\u30fc\u30b6\u30fc\u30c7\u30fc\u30bf\u306f\u500b\u4eba\u60c5\u5831\u3092\u542b\u3080\u3053\u3068\u3082\u3042\u3063\u3066\u3001\u3053\u306e\u69d8\u306a\u5f62\u3067\u516c\u958b\u3059\u308b\u306e\u306f\u826f\u304f\u306a\u3044\u306e\u3067\u3001\u672c\u756a\u3067\u884c\u3046\u5834\u5408\u306f\u6271\u3046\u30c7\u30fc\u30bf\u306b\u6c17\u3092\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><!-- notionvc: 66597f56-89c9-41d0-be47-ca63ca17f3d7 --><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\">[\n    {\n        \"id\": 1,\n        \"first_name\": \"daisuke\",\n        \"last_name\": \"takayama\",\n        \"username\": \"takayama daisuke\",\n        \"email\": \"webcyou@webcyou.com\"\n    },\n    {\n        \"id\": 2,\n        \"first_name\": \"Haruka\",\n        \"last_name\": \"Tanaka\",\n        \"username\": \"haruka_t\",\n        \"email\": \"haruka.tanaka@example.com\"\n    },\n   ...\n]<\/pre>\n<p>\u7d9a\u3044\u3066\u3001JSON\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u51e6\u7406\u3092\u884c\u3046load_users\u95a2\u6570\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30a2\u30d7\u30ea\u958b\u767a\u3067\u3001JSON\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3093\u3067\u305d\u306e\u30c7\u30fc\u30bf\u3092\u51e6\u7406\u3059\u308b\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3057\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u307e\u3093\u307e\u51e6\u7406\u304c\u6d41\u7528\u3067\u304d\u308b\u306e\u3067\u826f\u3044\u3067\u3059\u306d\u3002<\/p>\n<p>File::open\u3067\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3001\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<p>\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u3092\u6587\u5b57\u5217\u3068\u3057\u3066\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002\u3053\u3061\u3089\u3082\u8aad\u307f\u8fbc\u307f\u4e2d\u306b\u554f\u984c\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u3001\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<p>\u8aad\u307f\u8fbc\u3093\u3060\u6587\u5b57\u5217\u3092<code>serde_json<\/code><strong>\u3092\u4f7f\u7528\u3057\u3066<\/strong><code>User<\/code>\u69cb\u9020\u4f53\u306e\u30d9\u30af\u30c8\u30eb\u306b\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\uff08\u30d1\u30fc\u30b9\uff09\u3057\u307e\u3059\u3002<\/p>\n<p><code>User<\/code><strong>\u69cb\u9020\u4f53\u306e\u30d9\u30af\u30c8\u30eb\u3092\u3001<\/strong><code>User<\/code><strong>\u306eID\u3092\u30ad\u30fc\u3068\u3059\u308b<\/strong><code>HashMap<\/code>\u306b\u5909\u63db\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001ID\u306b\u57fa\u3065\u3044\u3066\u30e6\u30fc\u30b6\u30fc\u3092\u7c21\u5358\u306b\u691c\u7d22\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h5><span class=\"notion-enable-hover\" data-token-index=\"0\">JSON\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u3068\u69cb\u9020\u4f53\u3078\u306e\u5909\u63db<\/span><!-- notionvc: ed3fdb42-66be-44a5-ae4c-97eebcf3cdc8 --><\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\">use std::fs::File;\nuse std::io::Read;\nuse std::collections::HashMap;\n\nfn load_users() -&gt; HashMap&lt;u32, User&gt; {\n    let mut file = File::open(\"data\/user_list.json\").expect(\"File not found\");\n    let mut contents = String::new();\n    file.read_to_string(&amp;mut contents).expect(\"Something went wrong reading the file\");\n    let users: Vec&lt;User&gt; = serde_json::from_str(&amp;contents).unwrap();\n    users.into_iter().map(|user| (user.id, user)).collect()\n}<\/pre>\n<p>\u6700\u5f8c\u306f\u3001Actix Web\u306e\u30e1\u30a4\u30f3\u51e6\u7406\u3068\u306a\u308a\u307e\u3059\u3002\u30a6\u30a7\u30d6\u30b5\u30fc\u30d0\u30fc\u3092\u7acb\u3061\u4e0a\u3052\u3001\u30e6\u30fc\u30b6\u30fc\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u3092\u884c\u3046\u6a5f\u80fd\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<!-- notionvc: 055497e1-b075-49d6-b1ed-1b20011e6a01 --><\/p>\n<p><!-- notionvc: 97e56635-2e15-496b-a610-bf8116146349 --><\/p>\n<h5><span class=\"notion-enable-hover\" data-token-index=\"0\">Actix Web\u30cf\u30f3\u30c9\u30e9\u30fc\u306e\u4f5c\u6210<\/span><!-- notionvc: 300dc9c0-c31f-4719-a29a-0e7e356507e5 --><\/h5>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\">use actix_web::{web, App, HttpResponse, HttpServer, Responder};\n\nasync fn get_user(id: web::Path&lt;u32&gt;, data: web::Data&lt;HashMap&lt;u32, User&gt;&gt;) -&gt; impl Responder {\n    if let Some(user) = data.get(&amp;id.into_inner()) {\n        HttpResponse::Ok().json(user)\n    } else {\n        HttpResponse::NotFound().finish()\n    }\n}\n\n#[actix_web::main]\nasync fn main() -&gt; std::io::Result&lt;()&gt; {\n    let users = web::Data::new(load_users());\n    HttpServer::new(move || {\n        App::new()\n            .app_data(users.clone())\n            .route(\"\/user\/{id}\", web::get().to(get_user))\n    })\n    .bind(\"127.0.0.1:8080\")?\n    .run()\n    .await\n}<\/pre>\n<p>get_user \u95a2\u6570\u306f\u3001 \u7279\u5b9a\u306eID\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u3092HashMap\u304b\u3089\u691c\u7d22\u3057\u3001\u898b\u3064\u304b\u3063\u305f\u5834\u5408\u306f\u305d\u306e\u30e6\u30fc\u30b6\u30fc\u306e\u30c7\u30fc\u30bf\u3092JSON\u5f62\u5f0f\u3067\u8fd4\u5374\u3057\u307e\u3059\u3002\u30e6\u30fc\u30b6\u30fc\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u3001404 Not Found\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3057\u307e\u3059\u3002<!-- notionvc: e23d09fd-6cce-43de-a23c-4d8f5013663c --><\/p>\n<p><strong>main \u95a2\u6570<\/strong><\/p>\n<ul>\n<li>load_users\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u30e6\u30fc\u30b6\u30fc\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u307f\u3001web::Data\u3067\u30e9\u30c3\u30d7\u3057\u3066\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u6e21\u3057\u307e\u3059\u3002<\/li>\n<li>Actix Web\u30b5\u30fc\u30d0\u30fc\u3092\u8d77\u52d5\u3057\u3001&#8221;\/user\/{id}&#8221;\u306e\u30eb\u30fc\u30c8\u3067get_user\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u3088\u3046\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<p><!-- notionvc: cfddaa77-63c9-4b98-b996-9110a26a910c --><\/p>\n<p>\u3053\u3061\u3089\u3067\u5b8c\u6210\u3067\u3059\u3002\u307e\u305fPostman\u306a\u3069\u3067\u758e\u901a\u30c6\u30b9\u30c8\u884c\u3044\u307e\u3059\u3002HTTP GET\u30ea\u30af\u30a8\u30b9\u30c8\u3092<span class=\"notion-enable-hover\" spellcheck=\"false\" data-token-index=\"1\">&#8220;\/user\/{id}&#8221;<\/span>\u306b\u9001\u4fe1\u3059\u308b\u3068\u3001\u6307\u5b9a\u3055\u308c\u305fID\u306b\u5bfe\u5fdc\u3059\u308b\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u304cJSON\u5f62\u5f0f\u3067\u8fd4\u5374\u3055\u308c\u308b\u3088\u3046\u308c\u3070\u6210\u529f\u3067\u3059\u3002<!-- notionvc: 1e055b4a-ade2-40cc-b68e-355b52ead4c0 --><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\">cargo run\ncurl http:\/\/127.0.0.1:8080\/user\/3\n{\"id\":3,\"first_name\":\"Yuki\",\"last_name\":\"Sato\",\"username\":\"yuki_s\",\"email\":\"yuki.sato@example.com\"}<\/pre>\n<h4>\u5b9f\u88c5\u30bd\u30fc\u30b9<!-- notionvc: a2b6622c-241f-4cf5-94a9-c24938edfa27 --><\/h4>\n<p><a href=\"https:\/\/github.com\/rust-game-samples\/actix_web_sample\/tree\/main\/static_json\">Actix Web Static JSON<\/a><\/p>\n<p>\u975e\u5e38\u306b\u7c21\u5358\u306b\u884c\u3046\u4e8b\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u3053\u306e\u69d8\u306b\u3001\u65e2\u306b\u516c\u958b\u3055\u308c\u3066\u3044\u308b\u30de\u30b9\u30bf\u30fc\u30c7\u30fc\u30bf\u3067\u3042\u308c\u3070\u3001DB\u306e\u65b9\u306b\u6301\u305f\u305a\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u7ba1\u7406\u3057\u3001\u52d5\u7684\u306a\u30c7\u30fc\u30bf\u306b\u95a2\u3057\u3066\u306fDB\u3067\u6271\u3046\u3001\u30cf\u30a4\u30d6\u30ea\u30c3\u30c8\u306a\u904b\u7528\u3092\u884c\u3046\u306e\u3082\u826f\u3044\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u306d\u3002<\/p>\n<p>\u5f15\u304d\u7d9a\u304dActix Web\u3092\u4f7f\u3044\u8272\u3005\u3068\u4f5c\u6210\u3057\u3066\u3044\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u3002\u307e\u305f\u307e\u305f\u3041\u3002<\/p>\n<p><!-- notionvc: bf168f6a-100c-4266-b6ed-bd7dcd937c53 --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3069\u3082\u3067\u3059\u3002 \u524d\u56de\u300cRust \u2013 Actix Web \u00d7 MongoDB\u74b0\u5883\u3092\u30b5\u30af\u30c3\u3068\u8d77\u52d5\u300d\u3068\u3044\u3046\u8a18\u4e8b\u3092\u66f8\u304d\u307e\u3057\u305f\u304c\u3001\u516c\u958b\u3055\u308c\u3066\u3044\u308b\u5909\u5316\u306e\u306a\u3044\u56fa\u5b9a\u306e\u30c7\u30fc\u30bf\u3001\u6240\u8b02\u30de\u30b9\u30bf\u30fc\u30c7\u30fc\u30bf\u306b\u95a2\u3057\u3066\u306f\u3001mongoDB\u306e\u65b9\u306b\u4fdd\u5b58\u305b\u305a\u306bJSO [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11746,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[299],"tags":[331,166,332,327,333,45,328,178,78,114,334,118],"class_list":["post-11744","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-rust","tag-actix-web-2","tag-iphone","tag-json","tag-rust","tag-static","tag-web","tag-328","tag-178","tag-78","tag-114","tag-334","tag-118"],"_links":{"self":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/11744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11744"}],"version-history":[{"count":1,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/11744\/revisions"}],"predecessor-version":[{"id":11745,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/11744\/revisions\/11745"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/media\/11746"}],"wp:attachment":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}