首页 文章

如何设置go-swagger从注释生成规范

提问于
浏览
4

我在这里按照生成swagger规范的说明进行操作https://goswagger.io/generate/spec.html .

我有一个需要API的UI的现有项目 . 我想使用go swagger但我完全糊涂了https://github.com/go-swagger/go-swagger/tree/master/examples/todo-list我想设置它所以我在代码中添加注释然后运行命令swagger生成规范并且它会生成规范然而每当我运行它,它打印 {"swagger":"2.0","paths":{},"definitions":{}}

这是我运行它的命令

...com/projectFolder]$ swagger generate spec                                                
{"swagger":"2.0","paths":{},"definitions":{}}

我的项目结构如下

project/ 
  main.go
  api/
    router.go

在main.go中,我有这个注释

//go:generate swagger generate spec
package main

在我的一个处理程序上面的路由器中,我有这个注释

// swagger:route GET /profile
//
// Gets profile of user
//
//     Produces:
//     - application/json
//     - application/x-protobuf
//
//     Schemes: http, https, ws, wss
//
//     Security:
//       api_key:
//       oauth: read, write
//
//     Responses:
//       default: genericError
//       200: someResponse
//       422: validationError
r.GET("/profile", profileHandler

我一直试图设置api发生器一段时间 . 任何帮助深表感谢 . 如果您有设置经验,请告诉我您是如何做到的

1 回答

  • 2

    这可能是一个迟到的答案,但无论如何 . 我遇到了同样的问题,并且正在努力争取一些摇摇欲坠的文档;这就是我最终提出的:

    swagger generate 可以接受输入文件参数( -b main.go ,应用程序的入口点,从中自动查找所有其他注释)和输出文件( -o swagger.json

    所以在整体上你的命令应该是这样的:

    swagger generate spec -b ./main.go -o swagger.json

    在我的主要文件中,在它的顶部,我有一个不同于你的注释 . 它定义了 Headers 等基本项目属性:

    // Project title (the dot in the end matters).
    //
    // Project description.
    //
    //     Schemes: http
    //     Version: 0.1
    //
    //     Consumes:
    //     - application/json
    //
    //     Produces:
    //     - application/json
    //
    //
    // swagger:meta
    

相关问题