zoukankan      html  css  js  c++  java
  • Echo Swagger使用

    Echo Swagger使用

    1.源码编译

     go get github.com/swaggo/swag/cmd/swag
     
     cd github.com/swaggo/swag/cmd
     go build .  //生成swag二进制文件,可拷贝到/usr/local/go/bin目录,便于使用
    

    2.或者直接下载二进制文件
    https://github.com/swaggo/swag/releases //解压后,将swag文件拷贝到Path路径中

    3.controller中加入对应的注解

    // @Summary Book
    // @Description get book info
    // @Tags book
    // @Accept json
    // @Produce json
    // @Success 200 {string} string "Success"
    // @Failure 400 {string} string "Failed"
    // @Failure 404 {string} string "Not Found"
    // @Failure 500 {string} string "Internal Error"
    // @Router /contractMgmt/showcontracts [get]
    

    标签说明

    4.进入工程目录,执行如下命令

    swag init  //自动生成doc目录
    
    Goland 安装swag插件,可以实时预览!!!
    

    5.main.go中加入swagger中间件
    (1.)导包

    import	"github.com/swaggo/echo-swagger"
    import	_ "xxx/自己项目的目录/docs"  //该目录为swag init生成的doc目录
    

    (2.)main函数中加入swagger API相关的信息

    // @title Swagger Book API
    // @version 1.0
    // @description This is a echo swaagger api doc.
    // @termsOfService http://swagger.io/terms/
    
    // @contact.name API Support
    // @contact.url http://www.swagger.io/support
    // @contact.email support@swagger.io
    
    // @license.name Apache 2.0
    // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
    
    // @host petstore.swagger.io
    // @BasePath /v2
    e.GET("/swagger/*", echoSwagger.WrapHandler)  //echo-swagger中间件
    

    (3.)编译运行,访问页面

    go build .
    ./xxx   //运行二进制文件
    
    http://localhost:port/swagger/index.html
    
  • 相关阅读:
    Linux小命了(6)cd
    Linux小命令(5)mkdir
    性能测试(1)-开篇杂谈
    Linux小命令(4)ls
    Linux小命令(3)cat
    Linux小命令(2)man
    机器学习-学习方法
    flask-cache
    mysql 常用时间函数
    发现变化,拥抱变化
  • 原文地址:https://www.cnblogs.com/tomtellyou/p/12875748.html
Copyright © 2011-2022 走看看