{"id":147,"date":"2026-06-16T15:28:20","date_gmt":"2026-06-16T07:28:20","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=147"},"modified":"2026-06-16T15:28:20","modified_gmt":"2026-06-16T07:28:20","slug":"fastapi-%e8%b7%af%e5%be%84%e5%8f%82%e6%95%b0%e6%95%b0%e5%80%bc%e6%a0%a1%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/16\/fastapi-%e8%b7%af%e5%be%84%e5%8f%82%e6%95%b0%e6%95%b0%e5%80%bc%e6%a0%a1%e9%aa%8c\/","title":{"rendered":"FastAPI \u8def\u5f84\u53c2\u6570\u6570\u503c\u6821\u9a8c"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728FastAPI\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Pydantic\u6765\u5bf9\u8def\u5f84\u53c2\u6570\u8fdb\u884c\u6570\u503c\u9a8c\u8bc1\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>Path<\/code>\u51fd\u6570\u6765\u5b9a\u4e49\u8def\u5f84\u53c2\u6570\uff0c\u5e76\u6307\u5b9a\u5176\u7c7b\u578b\u548c\u9a8c\u8bc1\u89c4\u5219\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528FastAPI\u548cPydantic\u5bf9<em>paths<\/em>\u53c2\u6570\u8fdb\u884c\u6570\u503c\u4ee5\u81f3\u4e8e3000\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from fastapi import FastAPI, Path\nfrom pydantic import BaseModel, Field\n\napp = FastAPI()\n\nclass Item(BaseModel):\n    # \u8fd9\u91cc\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4f60\u7684\u6570\u636e\u6a21\u578b\n    pass\n\n@app.get(&quot;\/items\/{item_id}&quot;)\nasync def read_item(item_id: int = Path(..., gt=0, le=3000, description=&quot;Item ID must be between 1 and 3000&quot;)):\n    return {&quot;item_id&quot;: item_id}<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>item_id<\/code>\u53c2\u6570\uff0c\u5b83\u5fc5\u987b\u662f\u4e00\u4e2a\u6b63\u6574\u6570\u6570\uff0c\u4e14\u5c0f\u4e8e\u7b49\u4e8e3000\u3002\u5982\u679c\u7528\u6237\u63d0\u4f9b\u7684<code>item_id<\/code>\u4e0d\u6ee1\u8db3\u8fd9\u4e9b\u6761\u4ef6\uff0cFastAPI\u4f1a\u8fd4\u56de\u4e00\u4e2a422 Unprocessable Entity\u9519\u8bef\uff0c\u5e76\u7ed9\u6570\u636e\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u4f7f\u7528<code>Field<\/code>\u6765\u8bbe\u7f6e\u66f4\u591a\u7684\u9a8c\u8bc1\u89c4\u5219\uff0c\u4f8b\u5982<code>gt<\/code>\uff08\u5927\u4e8e\uff09\u3001<code>lt<\/code>\uff08\u5c0f\u4e8e\uff09\u3001<code>ge<\/code>\uff08\u5927\u4e8e\u7b49\u4e8e\uff09\u3001<code>le<\/code>\uff08\u5c0f\u4e8e\u7b49\u4e8e\uff09\u3001<code>multiple_of<\/code>\uff08\u6574\u9664\uff09\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff0c\u8fd9\u4e9b\u9a8c\u8bc1\u8c03\u5ea6\u662f\u5728\u670d\u52a1\u5c42\u8fdb\u884c validated\u7684\uff0c\u5b83\u4eec\u4e0d\u4f1a\u76f4\u63a5\u5f71\u54cd\u4f60\u7684API\u6587\u6863\u6216 OpenAPI Schema\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e0c\u671b\u8fd9\u4e2a\u7b54\u6848\u80fd\u5e6b\u52a9\u5230\u4f60\uff01\u5982\u679c\u8fd8\u6709\u4efb\u4f55 \u0432\u043e\u043f\u0440\u043e\u0441\u044b\uff0c\u8bf7\u968f\u610f\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728FastAPI\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Pydantic\u6765\u5bf9\u8def\u5f84\u53c2\u6570\u8fdb&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-fastapi-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}