我试图将swagger-ui与swagger-docs宝石一起使用。访问时,ui界面失败并显示以下错误。
"Can't read from file /apidocs/api-docs.json"
{
"apiVersion": "1.0",
"swaggerVersion": "1.2",
"basePath": "http://localhost:3000",
"apis": [
{
"path": "/apidocs/api/v1/users.{format}",
"description": "Users"
}
],
"authorizations": null,
"info": {
"title": "HAPIE API Documention",
"contact": "antarr.byrd@gmail.com",
"license": "Apache 2.0",
"licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
}
get '/docs' => redirect('/swagger/dist/index.html?url=/apidocs/api-docs.json')
# config/initializers/swagger-docs.rb
class Swagger::Docs::Config
def self.transform_path(path, api_version)
# Make a distinction between the APIs and API documentation paths.
"apidocs/#{path}"
end
end
Swagger::Docs::Config.base_api_controller = Api::V1::ApiController
Swagger::Docs::Config.register_apis({
"1.0" => {
:api_extension_type => :json,
# location where our api doc files will be generated, as of now we will store files under public directory
:api_file_path => "public/apidocs",
# base path url of our application
# while using production mode, point it to production url
:base_path => "http://localhost:3000",
# setting this option true tells swagger to clean all files generated in api_file_path directory before any files are generated
:clean_directory => true,
# As we are using Rails-API, our ApplicationController inherits ActionController::API instead of ActionController::Base
# Hence, we need to add ActionController::API instead of default ActionController::Base
:base_api_controller => Api::V1::ApiController,
# parent_controller needs to be specified if API controllers are inheriting some other controller than ApplicationController
:parent_controller => Api::V1::ApiController,
:attributes => {
:info => {
"title" => "HAPIE API Documention",
# "description" => "How Swagger works",
"contact" => "antarr.byrd@gmail.com",
"license" => "Apache 2.0",
"licenseUrl" => "http://www.apache.org/licenses/LICENSE-2.0.html"
}
}
}
})
答案 0 :(得分:3)
似乎不支持较新版本的swagger-ui。我假设你已经使用git子模块将Swagger添加到公共目录。所以转到你的public / swagger文件夹并检查旧标签。对我来说,v2.2.8正在运作。