{"id":94698,"date":"2026-08-15T00:32:46","date_gmt":"2026-08-14T16:32:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/94698.html"},"modified":"2026-08-15T00:32:46","modified_gmt":"2026-08-14T16:32:46","slug":"%e6%88%91%e5%ad%a6-fastapi-%e7%9a%84%e4%b8%80%e4%ba%9b%e5%bf%83%e5%be%97%ef%bc%9a%e4%bb%8e%e9%9b%b6%e5%88%b0%e8%b7%91%e9%80%9a%e7%ac%ac%e4%b8%80%e4%b8%aa%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/94698.html","title":{"rendered":"\u6211\u5b66 FastAPI \u7684\u4e00\u4e9b\u5fc3\u5f97\uff1a\u4ece\u96f6\u5230\u8dd1\u901a\u7b2c\u4e00\u4e2a\u63a5\u53e3"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u5b66 FastAPI&#xff0c;\u611f\u89c9\u8fd9\u4e2a\u4e1c\u897f\u771f\u7684\u633a\u6709\u610f\u601d\u7684\u3002\u4e4b\u524d\u6211\u7528\u8fc7 Flask&#xff0c;\u5199\u63a5\u53e3\u662f\u633a\u7b80\u5355&#xff0c;\u4f46\u6bcf\u6b21\u90fd\u8981\u81ea\u5df1\u641e\u6570\u636e\u6821\u9a8c\u3001\u5199\u6587\u6863&#xff0c;\u9ebb\u70e6\u5f97\u5f88\u3002\u540e\u6765\u542c\u670b\u53cb\u63a8\u8350 FastAPI&#xff0c;\u8bf4\u5b83&#034;\u5feb\u3001\u81ea\u52a8\u751f\u6210\u6587\u6863\u3001\u7c7b\u578b\u63d0\u793a\u5f88\u723d&#034;&#xff0c;\u6211\u5c31\u8bd5\u7740\u5b66\u4e86\u4e00\u4e0b\u3002\u8fd9\u7bc7\u6587\u7ae0\u7b97\u662f\u6211\u81ea\u5df1\u7684\u5b66\u4e60\u7b14\u8bb0\u5427&#xff0c;\u5c3d\u91cf\u7528\u5927\u767d\u8bdd\u628a\u6211\u8e29\u8fc7\u7684\u5751\u548c\u641e\u61c2\u7684\u4e1c\u897f\u5206\u4eab\u51fa\u6765\u3002<\/p>\n<h3>\u4e00\u3001FastAPI \u662f\u4e2a\u5565&#xff1f;<\/h3>\n<p>\u7b80\u5355\u8bf4&#xff0c;FastAPI \u5c31\u662f\u4e00\u4e2a\u7528 Python \u5199 Web API \u7684\u6846\u67b6&#xff0c;\u5e95\u5c42\u7528\u7684\u662f Starlette&#xff08;\u8d1f\u8d23\u7f51\u7edc\u90e8\u5206&#xff09;\u548c Pydantic&#xff08;\u8d1f\u8d23\u6570\u636e\u6821\u9a8c&#xff09;\u3002\u5b83\u6700\u5927\u7684\u7279\u70b9\u5c31\u662f\u5229\u7528 Python \u7684\u7c7b\u578b\u63d0\u793a&#xff0c;\u8ba9\u4f60\u5199\u51fd\u6570\u7684\u65f6\u5019\u987a\u4fbf\u628a\u53c2\u6570\u7c7b\u578b\u6807\u4e00\u4e0b&#xff0c;\u5b83\u5c31\u80fd\u81ea\u52a8\u5e2e\u4f60\u505a\u6570\u636e\u6821\u9a8c\u3001\u7c7b\u578b\u8f6c\u6362&#xff0c;\u751a\u81f3\u751f\u6210\u63a5\u53e3\u6587\u6863\u3002<\/p>\n<p>\u5bf9\u6211\u6765\u8bf4\u6700\u5438\u5f15\u6211\u7684\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4e0d\u7528\u624b\u5199\u6587\u6863&#xff1a;\u5199\u5b8c\u4ee3\u7801\u76f4\u63a5\u6253\u5f00 \/docs \u5c31\u80fd\u770b\u5230\u4e00\u4e2a\u53ef\u4ee5\u4ea4\u4e92\u7684 API \u6587\u6863\u9875\u9762&#xff0c;\u8fd8\u80fd\u76f4\u63a5\u5728\u4e0a\u9762\u6d4b\u8bd5\u63a5\u53e3&#xff0c;\u592a\u7701\u4e8b\u4e86\u3002<\/p>\n<\/li>\n<li>\n<p>\u6027\u80fd\u597d&#xff1a;\u5b98\u65b9\u8bf4\u6027\u80fd\u53ef\u4ee5\u8ddf Node.js\u3001Go \u6bd4&#xff0c;\u867d\u7136\u6211\u6ca1\u5b9e\u9645\u6d4b\u8fc7&#xff0c;\u4f46\u7528\u8d77\u6765\u786e\u5b9e\u611f\u89c9\u54cd\u5e94\u5f88\u5feb\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bed\u6cd5\u7b80\u5355&#xff1a;\u5199\u8d77\u6765\u8ddf Flask \u5dee\u4e0d\u591a&#xff0c;\u5b9a\u4e49\u8def\u7531\u5c31\u662f\u52a0\u4e2a\u88c5\u9970\u5668&#xff0c;\u5bf9\u65b0\u624b\u5f88\u53cb\u597d\u3002<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u6821\u9a8c\u5f3a&#xff1a;\u6bd4\u5982\u4f60\u5b9a\u4e49\u53c2\u6570\u662f int&#xff0c;\u7528\u6237\u4f20\u4e86 abc&#xff0c;\u5b83\u4f1a\u81ea\u52a8\u8fd4\u56de 422 \u9519\u8bef&#xff0c;\u4e0d\u7528\u81ea\u5df1\u5224\u65ad\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u6211\u89c9\u5f97 FastAPI \u7279\u522b\u9002\u5408\u5199\u5c0f\u578b API \u6216\u8005\u5feb\u901f\u505a\u539f\u578b&#xff0c;\u914d\u5408\u81ea\u52a8\u6587\u6863&#xff0c;\u524d\u540e\u7aef\u8054\u8c03\u7684\u65f6\u5019\u80fd\u7701\u5f88\u591a\u6c9f\u901a\u6210\u672c\u3002<\/p>\n<h3>\u4e8c\u3001\u6211\u7684\u7b2c\u4e00\u4e2a FastAPI \u7a0b\u5e8f<\/h3>\n<h4>1. \u73af\u5883\u51c6\u5907<\/h4>\n<p>\u6211\u662f\u5728\u672c\u5730\u7528\u865a\u62df\u73af\u5883\u8dd1\u7684&#xff0c;\u5efa\u8bae\u4f60\u4e5f\u8fd9\u4e48\u505a&#xff0c;\u907f\u514d\u88c5\u4e00\u5806\u5305\u628a\u7cfb\u7edf Python \u641e\u4e71\u3002<\/p>\n<p>python -m venv venv<\/p>\n<p>\u7136\u540e\u6fc0\u6d3b&#xff1a;<\/p>\n<ul>\n<li>\n<p>Windows: venv\\\\Scripts\\\\activate<\/p>\n<\/li>\n<li>\n<p>Mac\/Linux: source venv\/bin\/activate<\/p>\n<\/li>\n<\/ul>\n<h4>2. \u5b89\u88c5<\/h4>\n<p>\u5b89\u88c5 FastAPI \u548c\u4e00\u4e2a\u670d\u52a1\u5668\u3002FastAPI \u672c\u8eab\u4e0d\u5e26\u670d\u52a1\u5668&#xff0c;\u9700\u8981\u7528 ASGI \u670d\u52a1\u5668\u8dd1\u8d77\u6765&#xff0c;\u6700\u5e38\u7528\u7684\u662f uvicorn\u3002<\/p>\n<p>pip install fastapi &#034;uvicorn[standard]&#034;<\/p>\n<p>\u90a3\u4e2a [standard] \u4f1a\u88c5\u4e00\u4e9b\u989d\u5916\u4f9d\u8d56&#xff0c;\u6bd4\u5982\u6027\u80fd\u4f18\u5316&#xff0c;\u5efa\u8bae\u52a0\u4e0a\u3002<\/p>\n<h4>3. \u5199\u4ee3\u7801<\/h4>\n<p>\u65b0\u5efa\u4e00\u4e2a main.py&#xff0c;\u5185\u5bb9\u5f88\u7b80\u5355&#xff1a;<\/p>\n<p>from fastapi import FastAPI<\/p>\n<p>app &#061; FastAPI()<\/p>\n<p>&#064;app.get(&#034;\/&#034;)<br \/>\ndef read_root():<br \/>\n    return {&#034;message&#034;: &#034;Hello, FastAPI!&#034;}<\/p>\n<p>\u89e3\u91ca\u4e00\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\n<p>FastAPI() \u521b\u5efa\u5e94\u7528\u5b9e\u4f8b\u3002<\/p>\n<\/li>\n<li>\n<p>&#064;app.get(&#034;\/&#034;) \u8868\u793a\u8fd9\u662f\u4e00\u4e2a GET \u8bf7\u6c42&#xff0c;\u8def\u5f84\u662f\u6839\u8def\u5f84\u3002<\/p>\n<\/li>\n<li>\n<p>\u51fd\u6570\u91cc return \u4e00\u4e2a\u5b57\u5178&#xff0c;FastAPI \u4f1a\u81ea\u52a8\u8f6c\u6210 JSON \u8fd4\u56de\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>4. \u8dd1\u8d77\u6765<\/h4>\n<p>\u5728\u7ec8\u7aef\u6267\u884c&#xff1a;<\/p>\n<p>uvicorn main:app &#8211;reload<\/p>\n<ul>\n<li>\n<p>main \u662f\u6587\u4ef6\u540d&#xff08;\u4e0d\u542b .py&#xff09;<\/p>\n<\/li>\n<li>\n<p>app \u662f\u91cc\u9762\u90a3\u4e2a\u5b9e\u4f8b\u53d8\u91cf<\/p>\n<\/li>\n<li>\n<p>&#8211;reload \u662f\u70ed\u91cd\u8f7d&#xff0c;\u6539\u4ee3\u7801\u81ea\u52a8\u91cd\u542f&#xff0c;\u5f00\u53d1\u7684\u65f6\u5019\u5f88\u65b9\u4fbf\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u542f\u52a8\u540e\u4f1a\u770b\u5230&#xff1a;<\/p>\n<p>INFO:     Uvicorn running on http:\/\/127.0.0.1:8000<\/p>\n<p>\u6d4f\u89c8\u5668\u6253\u5f00 http:\/\/127.0.0.1:8000\/ \u5c31\u80fd\u770b\u5230 JSON \u8fd4\u56de\u3002<\/p>\n<p>\u91cd\u70b9\u6765\u4e86&#xff1a;\u6253\u5f00 http:\/\/127.0.0.1:8000\/docs&#xff0c;\u4f60\u4f1a\u770b\u5230\u4e00\u4e2a Swagger UI \u7684\u754c\u9762&#xff0c;\u4e0a\u9762\u6709\u4f60\u521a\u624d\u5199\u7684\u63a5\u53e3&#xff0c;\u70b9\u4e00\u4e0b &#034;Try it out&#034; \u5c31\u80fd\u76f4\u63a5\u6d4b\u8bd5\u3002\u6211\u7b2c\u4e00\u6b21\u770b\u5230\u8fd9\u4e2a\u7684\u65f6\u5019\u771f\u7684\u89c9\u5f97\u633a\u723d\u7684&#xff0c;\u518d\u4e5f\u4e0d\u7528\u5355\u72ec\u7528 Postman \u4e86\u3002<\/p>\n<h3>\u4e09\u3001\u8def\u7531\u548c\u8def\u5f84\u53c2\u6570<\/h3>\n<h4>\u8def\u7531\u662f\u5565&#xff1f;<\/h4>\n<p>\u8def\u7531\u5c31\u662f\u544a\u8bc9 FastAPI&#xff1a;\u5f53\u7528\u6237\u8bbf\u95ee\u67d0\u4e2a URL \u5e76\u4e14\u7528\u67d0\u4e2a HTTP \u65b9\u6cd5\u8bf7\u6c42\u65f6&#xff0c;\u5e94\u8be5\u6267\u884c\u54ea\u4e2a\u51fd\u6570\u3002FastAPI \u7528\u88c5\u9970\u5668\u6765\u5b9a\u4e49&#xff1a;<\/p>\n<ul>\n<li>\n<p>&#064;app.get() -&gt; GET \u8bf7\u6c42<\/p>\n<\/li>\n<li>\n<p>&#064;app.post() -&gt; POST \u8bf7\u6c42<\/p>\n<\/li>\n<li>\n<p>&#064;app.put() -&gt; PUT \u8bf7\u6c42<\/p>\n<\/li>\n<li>\n<p>&#064;app.delete() -&gt; DELETE \u8bf7\u6c42<\/p>\n<\/li>\n<\/ul>\n<p>\u6bd4\u5982&#xff1a;<\/p>\n<p>&#064;app.get(&#034;\/items&#034;)<br \/>\ndef list_items():<br \/>\n    return [&#034;item1&#034;, &#034;item2&#034;]<\/p>\n<p>\u8bbf\u95ee \/items \u5c31\u4f1a\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<h4>\u8def\u5f84\u53c2\u6570&#xff1a;URL \u91cc\u5e26\u53d8\u91cf<\/h4>\n<p>\u8def\u5f84\u53c2\u6570\u5c31\u662f URL \u8def\u5f84\u4e2d\u7684\u4e00\u90e8\u5206\u662f\u53ef\u53d8\u7684&#xff0c;\u6bd4\u5982 \/items\/123 \u91cc\u7684 123\u3002\u4f60\u53ef\u4ee5\u5728\u8def\u5f84\u4e2d\u7528 {\u53c2\u6570\u540d} \u8868\u793a&#xff0c;\u7136\u540e\u5728\u51fd\u6570\u53c2\u6570\u91cc\u7528\u540c\u6837\u7684\u540d\u5b57\u63a5\u6536\u3002<\/p>\n<h5>\u57fa\u7840\u7528\u6cd5<\/h5>\n<p>&#064;app.get(&#034;\/items\/{item_id}&#034;)<br \/>\ndef read_item(item_id: int):<br \/>\n    return {&#034;item_id&#034;: item_id, &#034;type&#034;: str(type(item_id).__name__)}<\/p>\n<p>\u8fd9\u91cc\u6211\u7279\u610f\u8fd4\u56de\u4e86 type&#xff0c;\u5c31\u662f\u60f3\u9a8c\u8bc1\u4e00\u4e0b\u7c7b\u578b\u8f6c\u6362\u3002\u4f60\u8bbf\u95ee http:\/\/127.0.0.1:8000\/items\/42&#xff0c;\u8fd4\u56de&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;item_id&#034;: 42,<br \/>\n  &#034;type&#034;: &#034;int&#034;<br \/>\n}<\/p>\n<p>\u770b\u5230\u6ca1&#xff0c;item_id \u5df2\u7ecf\u662f\u6574\u6570 42&#xff0c;\u4e0d\u662f\u5b57\u7b26\u4e32 &#034;42&#034;\u3002\u8fd9\u5c31\u662f\u7c7b\u578b\u63d0\u793a\u7684\u4f5c\u7528&#xff1a;FastAPI \u81ea\u52a8\u628a\u8def\u5f84\u53c2\u6570\u8f6c\u6210\u4e86\u4f60\u6307\u5b9a\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u5982\u679c\u4f60\u8bbf\u95ee \/items\/abc&#xff0c;\u5b83\u4f1a\u8fd4\u56de 422 \u9519\u8bef&#xff0c;\u56e0\u4e3a abc \u6ca1\u6cd5\u8f6c\u6210 int\u3002\u8fd9\u4e2a\u9519\u8bef\u4fe1\u606f\u8fd8\u5f88\u8be6\u7ec6&#xff0c;\u4f1a\u544a\u8bc9\u4f60\u54ea\u4e2a\u5b57\u6bb5\u7c7b\u578b\u4e0d\u5bf9\u3002\u6211\u7528 Flask \u7684\u65f6\u5019\u5f97\u81ea\u5df1\u5199\u4e00\u5806\u5224\u65ad&#xff0c;\u73b0\u5728\u5b8c\u5168\u4e0d\u7528\u7ba1\u3002<\/p>\n<h5>\u591a\u4e2a\u8def\u5f84\u53c2\u6570<\/h5>\n<p>\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a&#xff1a;<\/p>\n<p>&#064;app.get(&#034;\/users\/{user_id}\/posts\/{post_id}&#034;)<br \/>\ndef get_user_post(user_id: int, post_id: int):<br \/>\n    return {&#034;user_id&#034;: user_id, &#034;post_id&#034;: post_id}<\/p>\n<p>\u8bbf\u95ee \/users\/1\/posts\/99 \u5c31\u4f1a\u8fd4\u56de&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;user_id&#034;: 1,<br \/>\n  &#034;post_id&#034;: 99<br \/>\n}<\/p>\n<h5>\u4e00\u4e2a\u6211\u8e29\u8fc7\u7684\u5751&#xff1a;\u56fa\u5b9a\u8def\u7531\u548c\u52a8\u6001\u8def\u7531\u987a\u5e8f<\/h5>\n<p>\u521a\u5f00\u59cb\u5b66\u7684\u65f6\u5019&#xff0c;\u6211\u5199\u4e86\u4e00\u4e2a \/users\/me \u548c \/users\/{user_id}&#xff0c;\u7ed3\u679c\u8bbf\u95ee \/users\/me \u4e00\u76f4\u62a5 422 \u9519\u8bef\u3002\u540e\u6765\u624d\u53d1\u73b0\u662f\u8def\u7531\u987a\u5e8f\u95ee\u9898\u3002<\/p>\n<p>FastAPI \u4f1a\u6309\u7167\u4ee3\u7801\u91cc\u5b9a\u4e49\u7684\u987a\u5e8f\u4f9d\u6b21\u5339\u914d\u3002\u5982\u679c\u4f60\u628a \/users\/{user_id} \u5199\u5728\u524d\u9762&#xff0c;\u90a3\u4e48\u8bbf\u95ee \/users\/me \u65f6&#xff0c;me \u4f1a\u88ab\u5f53\u6210 user_id \u7684\u503c&#xff0c;\u7136\u540e\u56e0\u4e3a\u7c7b\u578b\u662f int \u800c\u8f6c\u6362\u5931\u8d25\u3002<\/p>\n<p>\u6b63\u786e\u505a\u6cd5&#xff1a;\u628a\u56fa\u5b9a\u8def\u7531 \/users\/me \u5199\u5728\u52a8\u6001\u8def\u7531 \/users\/{user_id} \u524d\u9762\u3002<\/p>\n<p>&#064;app.get(&#034;\/users\/me&#034;)<br \/>\ndef read_current_user():<br \/>\n    return {&#034;user_id&#034;: &#034;the current user&#034;}<\/p>\n<p>&#064;app.get(&#034;\/users\/{user_id}&#034;)<br \/>\ndef read_user(user_id: int):<br \/>\n    return {&#034;user_id&#034;: user_id}<\/p>\n<p>\u8fd9\u6837 me \u4f1a\u88ab\u56fa\u5b9a\u8def\u7531\u6355\u83b7&#xff0c;\u5176\u4ed6\u6570\u5b57\u624d\u4f1a\u8d70\u52a8\u6001\u8def\u7531\u3002\u8fd9\u4e2a\u5751\u6211\u89c9\u5f97\u65b0\u624b\u5f88\u5bb9\u6613\u8e29&#xff0c;\u6240\u4ee5\u4e13\u95e8\u63d0\u4e00\u4e0b\u3002<\/p>\n<h5>\u7528\u679a\u4e3e\u9650\u5236\u8def\u5f84\u53c2\u6570\u7684\u53d6\u503c<\/h5>\n<p>\u6709\u65f6\u5019\u4f60\u5e0c\u671b\u8def\u5f84\u53c2\u6570\u53ea\u80fd\u662f\u51e0\u4e2a\u56fa\u5b9a\u503c&#xff0c;\u6bd4\u5982\u6a21\u578b\u540d\u79f0\u3002\u53ef\u4ee5\u7528 Python \u7684 Enum \u6765\u5b9a\u4e49\u3002<\/p>\n<p>from enum import Enum<\/p>\n<p>class ModelName(str, Enum):<br \/>\n    alexnet &#061; &#034;alexnet&#034;<br \/>\n    resnet &#061; &#034;resnet&#034;<br \/>\n    lenet &#061; &#034;lenet&#034;<\/p>\n<p>&#064;app.get(&#034;\/models\/{model_name}&#034;)<br \/>\ndef get_model(model_name: ModelName):<br \/>\n    if model_name is ModelName.alexnet:<br \/>\n        return {&#034;model_name&#034;: model_name, &#034;message&#034;: &#034;Deep Learning FTW!&#034;}<br \/>\n    if model_name.value &#061;&#061; &#034;lenet&#034;:<br \/>\n        return {&#034;model_name&#034;: model_name, &#034;message&#034;: &#034;LeCNN all the images&#034;}<br \/>\n    return {&#034;model_name&#034;: model_name, &#034;message&#034;: &#034;Have some residuals&#034;}<\/p>\n<p>\u8fd9\u6837\u7528\u6237\u53ea\u80fd\u4f20 alexnet\u3001resnet\u3001lenet&#xff0c;\u4f20\u522b\u7684\u4f1a\u8fd4\u56de 422\u3002\u6211\u7528\u8fd9\u4e2a\u7279\u6027\u505a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b\u9009\u62e9\u63a5\u53e3&#xff0c;\u611f\u89c9\u5f88\u5b9e\u7528\u3002<\/p>\n<h5>\u8def\u5f84\u53c2\u6570\u5305\u542b\u659c\u6760\u7684\u60c5\u51b5<\/h5>\n<p>\u5982\u679c\u4f60\u7684\u8def\u5f84\u53c2\u6570\u672c\u8eab\u8981\u5305\u542b \/&#xff0c;\u6bd4\u5982\u6587\u4ef6\u8def\u5f84&#xff0c;\u53ef\u4ee5\u7528 :path \u8f6c\u6362\u5668\u3002<\/p>\n<p>&#064;app.get(&#034;\/files\/{file_path:path}&#034;)<br \/>\ndef read_file(file_path: str):<br \/>\n    return {&#034;file_path&#034;: file_path}<\/p>\n<p>\u8bbf\u95ee \/files\/images\/photo.jpg&#xff0c;file_path \u5c31\u4f1a\u662f images\/photo.jpg\u3002\u8fd9\u4e2a\u9002\u5408\u505a\u6587\u4ef6\u4e0b\u8f7d\u6216\u8005\u8bfb\u53d6\u76ee\u5f55\u7ed3\u6784\u3002<\/p>\n<p>\u4e0d\u8fc7\u8981\u6ce8\u610f&#xff0c;\u8fd9\u79cd&#034;\u8d2a\u5a6a&#034;\u7684\u53c2\u6570\u6700\u597d\u653e\u5728\u8def\u7531\u672b\u5c3e&#xff0c;\u4e0d\u7136\u4f1a\u628a\u540e\u9762\u7684\u8def\u7531\u90fd\u5403\u6389\u3002<\/p>\n<h3>\u56db\u3001\u52a8\u624b\u7ec3\u4e00\u7ec3<\/h3>\n<p>\u5efa\u8bae\u4f60\u81ea\u5df1\u8bd5\u7740\u52a0\u4e00\u4e2a\u8def\u7531&#xff1a;<\/p>\n<p>&#064;app.get(&#034;\/greet\/{name}&#034;)<br \/>\ndef greet(name: str):<br \/>\n    return {&#034;message&#034;: f&#034;Hello, {name}!&#034;}<\/p>\n<p>\u7136\u540e\u8bbf\u95ee http:\/\/127.0.0.1:8000\/greet\/FastAPI&#xff0c;\u4f1a\u8fd4\u56de&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;message&#034;: &#034;Hello, FastAPI!&#034;<br \/>\n}<\/p>\n<p>\u518d\u53bb \/docs \u9875\u9762\u91cc\u73a9\u4e00\u73a9&#xff0c;\u8f93\u5165\u4e0d\u540c\u7684\u540d\u5b57&#xff0c;\u770b\u770b\u54cd\u5e94\u53d8\u5316\u3002\u8fd9\u6837\u4f60\u5c31\u80fd\u611f\u53d7\u5230\u81ea\u52a8\u6587\u6863\u7684\u597d\u5904\u4e86\u3002<\/p>\n<h3>\u6700\u540e<\/h3>\n<p>FastAPI \u7ed9\u6211\u7684\u611f\u89c9\u5c31\u662f&#xff1a;\u4f60\u5199\u4ee3\u7801\u7684\u65f6\u5019\u628a\u7c7b\u578b\u5199\u6e05\u695a&#xff0c;\u5b83\u5c31\u80fd\u5e2e\u4f60\u505a\u5f88\u591a\u4e8b\u3002\u8def\u5f84\u53c2\u6570\u3001\u67e5\u8be2\u53c2\u6570\u3001\u8bf7\u6c42\u4f53\u8fd9\u4e9b&#xff0c;\u90fd\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u63d0\u793a\u81ea\u52a8\u6821\u9a8c\u548c\u8f6c\u6362&#xff0c;\u800c\u4e14\u6587\u6863\u662f\u81ea\u52a8\u751f\u6210\u7684&#xff0c;\u7701\u4e86\u4e0d\u5c11\u4e8b\u3002<\/p>\n<p>\u6211\u76ee\u524d\u53ea\u5b66\u4e86\u8def\u7531\u548c\u8def\u5f84\u53c2\u6570&#xff0c;\u540e\u9762\u8fd8\u8981\u5b66\u67e5\u8be2\u53c2\u6570\u3001\u8bf7\u6c42\u4f53\u3001Pydantic \u6a21\u578b\u8fd9\u4e9b\u3002\u7b49\u5b66\u5b8c\u4e86\u518d\u7ee7\u7eed\u5206\u4eab\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4e5f\u5728\u5b66 FastAPI&#xff0c;\u6b22\u8fce\u4e00\u8d77\u4ea4\u6d41&#xff0c;\u6216\u8005\u76f4\u63a5\u53bb\u770b\u5b98\u65b9\u6587\u6863&#xff0c;\u5199\u5f97\u633a\u6e05\u695a\u7684\u3002<\/p>\n<h3>\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<p>from fastapi import FastAPI<br \/>\nfrom enum import Enum<\/p>\n<p>app &#061; FastAPI()<\/p>\n<p>class ModelName(str, Enum):<br \/>\n    alexnet &#061; &#034;alexnet&#034;<br \/>\n    resnet &#061; &#034;resnet&#034;<br \/>\n    lenet &#061; &#034;lenet&#034;<\/p>\n<p>&#064;app.get(&#034;\/&#034;)<br \/>\ndef read_root():<br \/>\n    return {&#034;message&#034;: &#034;Hello, FastAPI!&#034;}<\/p>\n<p>&#064;app.get(&#034;\/items\/{item_id}&#034;)<br \/>\ndef read_item(item_id: int):<br \/>\n    return {&#034;item_id&#034;: item_id, &#034;type&#034;: str(type(item_id).__name__)}<\/p>\n<p>&#064;app.get(&#034;\/users\/me&#034;)<br \/>\ndef read_current_user():<br \/>\n    return {&#034;user_id&#034;: &#034;the current user&#034;}<\/p>\n<p>&#064;app.get(&#034;\/users\/{user_id}&#034;)<br \/>\ndef read_user(user_id: int):<br \/>\n    return {&#034;user_id&#034;: user_id}<\/p>\n<p>&#064;app.get(&#034;\/models\/{model_name}&#034;)<br \/>\ndef get_model(model_name: ModelName):<br \/>\n    if model_name is ModelName.alexnet:<br \/>\n        return {&#034;model_name&#034;: model_name, &#034;message&#034;: &#034;Deep Learning FTW!&#034;}<br \/>\n    if model_name.value &#061;&#061; &#034;lenet&#034;:<br \/>\n        return {&#034;model_name&#034;: model_name, &#034;message&#034;: &#034;LeCNN all the images&#034;}<br \/>\n    return {&#034;model_name&#034;: model_name, &#034;message&#034;: &#034;Have some residuals&#034;}<\/p>\n<p>&#064;app.get(&#034;\/files\/{file_path:path}&#034;)<br \/>\ndef read_file(file_path: str):<br \/>\n    return {&#034;file_path&#034;: file_path}<\/p>\n<p>\u8fd0\u884c&#xff1a;<\/p>\n<p>uvicorn main:app &#8211;reload<\/p>\n<p>\u7136\u540e\u6253\u5f00 http:\/\/127.0.0.1:8000\/docs \u73a9\u8d77\u6765\u5427<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u5b66 FastAPI&#xff0c;\u611f\u89c9\u8fd9\u4e2a\u4e1c\u897f\u771f\u7684\u633a\u6709\u610f\u601d\u7684\u3002\u4e4b\u524d\u6211\u7528\u8fc7 Flask&#xff0c;\u5199\u63a5\u53e3\u662f\u633a\u7b80\u5355&#xff0c;\u4f46\u6bcf\u6b21\u90fd\u8981\u81ea\u5df1\u641e\u6570\u636e\u6821\u9a8c\u3001\u5199\u6587\u6863&#xff0c;\u9ebb\u70e6\u5f97\u5f88\u3002\u540e\u6765\u542c\u670b\u53cb\u63a8\u8350 FastAPI&#xff0c;\u8bf4\u5b83\\&#8221;\u5feb\u3001\u81ea\u52a8\u751f\u6210\u6587\u6863\u3001\u7c7b\u578b\u63d0\u793a\u5f88\u723d\\&#8221;&#xff0c;\u6211\u5c31\u8bd5\u7740\u5b66\u4e86\u4e00\u4e0b\u3002\u8fd9\u7bc7\u6587\u7ae0\u7b97\u662f\u6211\u81ea\u5df1\u7684\u5b66\u4e60\u7b14\u8bb0\u5427&#xff0c;\u5c3d\u91cf\u7528\u5927\u767d\u8bdd\u628a\u6211\u8e29\u8fc7\u7684\u5751\u548c\u641e\u61c2\u7684\u4e1c\u897f\u5206\u4eab\u51fa\u6765\u3002\u4e00\u3001FastAPI \u662f\u4e2a\u5565&#xff1f;\u7b80\u5355\u8bf4&#xff0c;FastAP<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10336,666],"topic":[],"class_list":["post-94698","post","type-post","status-publish","format-standard","hentry","category-server","tag-10336","tag-microsoft"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u6211\u5b66 FastAPI \u7684\u4e00\u4e9b\u5fc3\u5f97\uff1a\u4ece\u96f6\u5230\u8dd1\u901a\u7b2c\u4e00\u4e2a\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/94698.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u6211\u5b66 FastAPI \u7684\u4e00\u4e9b\u5fc3\u5f97\uff1a\u4ece\u96f6\u5230\u8dd1\u901a\u7b2c\u4e00\u4e2a\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6700\u8fd1\u5728\u5b66 FastAPI&#xff0c;\u611f\u89c9\u8fd9\u4e2a\u4e1c\u897f\u771f\u7684\u633a\u6709\u610f\u601d\u7684\u3002\u4e4b\u524d\u6211\u7528\u8fc7 Flask&#xff0c;\u5199\u63a5\u53e3\u662f\u633a\u7b80\u5355&#xff0c;\u4f46\u6bcf\u6b21\u90fd\u8981\u81ea\u5df1\u641e\u6570\u636e\u6821\u9a8c\u3001\u5199\u6587\u6863&#xff0c;\u9ebb\u70e6\u5f97\u5f88\u3002\u540e\u6765\u542c\u670b\u53cb\u63a8\u8350 FastAPI&#xff0c;\u8bf4\u5b83&quot;\u5feb\u3001\u81ea\u52a8\u751f\u6210\u6587\u6863\u3001\u7c7b\u578b\u63d0\u793a\u5f88\u723d&quot;&#xff0c;\u6211\u5c31\u8bd5\u7740\u5b66\u4e86\u4e00\u4e0b\u3002\u8fd9\u7bc7\u6587\u7ae0\u7b97\u662f\u6211\u81ea\u5df1\u7684\u5b66\u4e60\u7b14\u8bb0\u5427&#xff0c;\u5c3d\u91cf\u7528\u5927\u767d\u8bdd\u628a\u6211\u8e29\u8fc7\u7684\u5751\u548c\u641e\u61c2\u7684\u4e1c\u897f\u5206\u4eab\u51fa\u6765\u3002\u4e00\u3001FastAPI \u662f\u4e2a\u5565&#xff1f;\u7b80\u5355\u8bf4&#xff0c;FastAP\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/94698.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-14T16:32:46+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94698.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/94698.html\",\"name\":\"\u6211\u5b66 FastAPI \u7684\u4e00\u4e9b\u5fc3\u5f97\uff1a\u4ece\u96f6\u5230\u8dd1\u901a\u7b2c\u4e00\u4e2a\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-14T16:32:46+00:00\",\"dateModified\":\"2026-08-14T16:32:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/94698.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/94698.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94698.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u6211\u5b66 FastAPI \u7684\u4e00\u4e9b\u5fc3\u5f97\uff1a\u4ece\u96f6\u5230\u8dd1\u901a\u7b2c\u4e00\u4e2a\u63a5\u53e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u6211\u5b66 FastAPI \u7684\u4e00\u4e9b\u5fc3\u5f97\uff1a\u4ece\u96f6\u5230\u8dd1\u901a\u7b2c\u4e00\u4e2a\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/94698.html","og_locale":"zh_CN","og_type":"article","og_title":"\u6211\u5b66 FastAPI \u7684\u4e00\u4e9b\u5fc3\u5f97\uff1a\u4ece\u96f6\u5230\u8dd1\u901a\u7b2c\u4e00\u4e2a\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6700\u8fd1\u5728\u5b66 FastAPI&#xff0c;\u611f\u89c9\u8fd9\u4e2a\u4e1c\u897f\u771f\u7684\u633a\u6709\u610f\u601d\u7684\u3002\u4e4b\u524d\u6211\u7528\u8fc7 Flask&#xff0c;\u5199\u63a5\u53e3\u662f\u633a\u7b80\u5355&#xff0c;\u4f46\u6bcf\u6b21\u90fd\u8981\u81ea\u5df1\u641e\u6570\u636e\u6821\u9a8c\u3001\u5199\u6587\u6863&#xff0c;\u9ebb\u70e6\u5f97\u5f88\u3002\u540e\u6765\u542c\u670b\u53cb\u63a8\u8350 FastAPI&#xff0c;\u8bf4\u5b83\"\u5feb\u3001\u81ea\u52a8\u751f\u6210\u6587\u6863\u3001\u7c7b\u578b\u63d0\u793a\u5f88\u723d\"&#xff0c;\u6211\u5c31\u8bd5\u7740\u5b66\u4e86\u4e00\u4e0b\u3002\u8fd9\u7bc7\u6587\u7ae0\u7b97\u662f\u6211\u81ea\u5df1\u7684\u5b66\u4e60\u7b14\u8bb0\u5427&#xff0c;\u5c3d\u91cf\u7528\u5927\u767d\u8bdd\u628a\u6211\u8e29\u8fc7\u7684\u5751\u548c\u641e\u61c2\u7684\u4e1c\u897f\u5206\u4eab\u51fa\u6765\u3002\u4e00\u3001FastAPI \u662f\u4e2a\u5565&#xff1f;\u7b80\u5355\u8bf4&#xff0c;FastAP","og_url":"https:\/\/www.wsisp.com\/helps\/94698.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-14T16:32:46+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/94698.html","url":"https:\/\/www.wsisp.com\/helps\/94698.html","name":"\u6211\u5b66 FastAPI \u7684\u4e00\u4e9b\u5fc3\u5f97\uff1a\u4ece\u96f6\u5230\u8dd1\u901a\u7b2c\u4e00\u4e2a\u63a5\u53e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-14T16:32:46+00:00","dateModified":"2026-08-14T16:32:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/94698.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/94698.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/94698.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u6211\u5b66 FastAPI \u7684\u4e00\u4e9b\u5fc3\u5f97\uff1a\u4ece\u96f6\u5230\u8dd1\u901a\u7b2c\u4e00\u4e2a\u63a5\u53e3"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=94698"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94698\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=94698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=94698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=94698"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=94698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}