新手问题 Swagger UI 不解析生成的 JSON 文件

pinewong · January 12, 2017 · Last by liqiang123 replied at July 26, 2021 · 6014 hits

使用 swagger-docs 这个 gem,当执行完 rake swagger:docs 之后,api-docs.json 显示如下:

然后手动引入 swagger-ui 的 dist 文件夹到 public 目录,修改文件夹名字为 swagger-ui, 访问 /public/swagger-ui/index.html 文件,输入生成的 api-docs.json 路径,却显示不了 api 文档(没有 controller 的写的 api 文档)

请问大家,有哪步做错了的吗?

附上 initializers 里的 swagger 设置(其实就是照抄巴爷的,但是依然不行)

module Swagger
  module Docs
    class Config
      def self.base_api_controller
        ActionController::API
      end
    end
    Config.register_apis('1.0' =>
      {
        # the extension used for the API
        :api_extension_type => :json,
        # the output location where your .json files are written to
        :api_file_path => 'public/swagger_doc',
        # Ability to setup base controller for each api version. Api::V1::SomeController for example.
        :parent_controller => ActionController::API,
        # add custom attributes to api-docs
        :attributes => {
          :info => {
            'title' => 'Bayetech Rails API App',
            'description' => 'A grape replacement backend for iOS, Android and Wechat applet.',
            'termsOfServiceUrl' => 'http://www.bayekeji.com/contact',
            'contact' => '[email protected]'
          }
        }
      })
  end
end

感谢关注巴爷科技的微信小应用商城,这个 gem 确实挺难用的,主要坑有俩个,一个是生成 swagger 的时候,需要改public/swagger_doc/api-docs.json,basePath 改成http://localhost:3000/swagger_doc,另外一个是要允许跨域传送

#2 楼 @ericguo 谢谢,可以用

You need to Sign in before reply, if you don't have an account, please Sign up first.