{"id":709,"date":"2023-11-06T09:01:01","date_gmt":"2023-11-06T01:01:01","guid":{"rendered":""},"modified":"2023-11-06T09:01:01","modified_gmt":"2023-11-06T01:01:01","slug":"gin\u6846\u67b6\u914d\u7f6eswagger\u63a5\u53e3\u6587\u6863","status":"publish","type":"post","link":"https:\/\/mushiming.com\/709.html","title":{"rendered":"gin\u6846\u67b6\u914d\u7f6eswagger\u63a5\u53e3\u6587\u6863"},"content":{"rendered":"
\/\/ swagger\u5305\u4ee5\u53ca\u7248\u672c<\/span>\ngithub.com\/go<\/span>-openapi\/swag v0.19<\/span>.15<\/span> \/\/ indirect<\/span>\ngithub.com\/swaggo\/files v1.0<\/span>.0<\/span> \/\/ indirect<\/span>\ngithub.com\/swaggo\/gin-swagger v1.5<\/span>.3<\/span> \/\/ indirect<\/span>\ngithub.com\/swaggo\/swag v1.8<\/span>.10<\/span> \/\/ indirect<\/span>\n<\/code><\/pre>\nimport<\/span> (\n\t\"github.com\/gin-gonic\/gin\"<\/span>\n\tswaggerFiles \"github.com\/swaggo\/files\"<\/span>\n\tginSwagger \"github.com\/swaggo\/gin-swagger\"<\/span>\n)\n\nfunc<\/span> InitRouter<\/span>(router *gin.Engine)<\/span><\/span> {\n\trootRoute := router.Group(\"api\"<\/span>)\n\trootRoute.GET(\"swagger\/*any\"<\/span>, ginSwagger.WrapHandler(swaggerFiles.Handler))\n\tRegisterProjectRoute(rootRoute)\n}\n\n<\/code><\/pre>\n\u6ce8\u610f\u70b9:<\/p>\n
\n- \u5982\u679c
swag init<\/code>\u63d0\u793a\u65e0\u547d\u4ee4\uff0c\u9700\u8981\u914d\u7f6eswag\u7684\u73af\u5883\u53d8\u91cf\uff0c\u4e00\u822c\u662fgo env\u4e2d\u7684GOPATH<\/li>\n- \u5982\u679cmain.go\u4e0d\u518d\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\uff0c\u5219\u6267\u884c
swag init<\/code>\u751f\u6210swagger\u6587\u4ef6\u4f1a\u62a5\u9519\uff0c\u9700\u8981`swag init --parseDependency -g \u5165\u53e3main\u6587\u4ef6<\/li>\n- \u9700\u8981\u5728main.go\u4e5f\u5c31\u662f\u9879\u76ee\u542f\u52a8\u6587\u4ef6\u4e0b\u914d\u7f6eswag init\u751f\u6210\u7684\u6587\u4ef6, \u5982:
_ \"foo\/docs\"<\/code>\u6ce8\u610f\u4e0b\u5212\u7ebf\u5fc5\u52a0<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"gin\u6846\u67b6\u914d\u7f6eswagger\u63a5\u53e3\u6587\u6863\u7b2c\u4e09\u65b9\u5305 \u6ce8\u610f\u70b9: \u5982\u679cmain.go\u4e0d\u518d\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\uff0c\u5219\u6267\u884cswag init\u751f\u6210swagger\u6587\u4ef6\u4f1a\u62a5\u9519\uff0c\u9700\u8981`...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/709"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=709"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/709\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}