基本步骤:
1.安装所需gem
在 Gemfile 中添加 grape-swagger 和 grape-swagger-rails 这两个 gem。

(注明版本号)
(grape-swagger-rails是 Swagger UI 的 Rails Engine , 也是必备的组件)
2.创建基本框架

application_api.rb 是最底层的,里面放最通用的配置,所以在这里 require 'grape-swagger',这样不用每次都 require 了

base_api.rb中挂载其余所有api

add_swagger_documentation中声明的三个参数:
(1).api_version 路径,生成文档
(2).hide_documentation_path: 隐藏文件路径
(3).hide_format 去掉url后面的格式后缀(.json)
3.配置Swagger_UI
在config/initializers 目录下添加 swagger.rb 文件

在route中声明 apidoc 的路径

最后通过localhost:3000/apidoc访问api文档。
网友评论