swagger-docs :
https://github.com/richhollis/swagger-docs
swagger-docs-sample:
https://github.com/richhollis/swagger-docs-sample/blob/master/README.md
swagger-ui: https://github.com/wordnik/swagger-ui
1、vim Gemfile 添加 gem ‘swagger-docs‘ 然后
swagger-docs
bundle 或者直接
gem install
2、cd rails项目目录; vim
config/initializers/swagger_docs.rb , 并添加保存如下内容:
Swagger::Docs::Config.register_apis({
"1.0" => {:base_path => "http://localhost:3000", :api_file_path => "public"}
})
3、在某个controller中添加:
swagger_controller :user_api, "UserApi"
swagger_api :index do
summary "test"
response :unauthorized
response :not_acceptable
end
def index
render :json => {result:"Success"}, :status => 200
end
4、rake swagger:docs
5、rails s
6、cd public
7、git clone https://github.com/wordnik/swagger-ui.git
8、vim swagger-ui/dist/index.html
将 url 修改为 http://localhost:3000/api-docs.json
window.swaggerUi = new SwaggerUi({
url: "http://localhost:3000/api-docs.json",
//http://petstore.swagger.wordnik.com/api/api-docs
dom_id: "swagger-ui-container",
supportedSubmitMethods: [‘get‘, ‘post‘, ‘put‘, ‘delete‘],
... ... ...
9、浏览器中输入 http://localhost:3000/swagger-ui/dist/index.html
即可查看并测试api
如果页面打开慢,vim public/swagger-ui/dist/index.html ,
将 ...fonts.googleapis.com/css...这行代码注释掉。
注: config/routes.rb 中 swagger对应的route 不要用match, 否则会报错
时间: 2024-10-16 14:46:30