为已经托管的API创建Swagger文档

时间:2019-06-26 08:45:58

标签: swagger

我有一个非标准的API,我想为其创建Swagger文档。既然它离标准还差得远,我将无法生成任何东西,相反,我将不得不手动进行。

我按照说明进行了操作,并创建了“ hello-world”项目,但我一直试图对其进行编辑以调用自己的API。一个简单的GET端点,返回一个JSON对象列表。

我假设我可以忽略与控制器和模拟等有关的所有事情,因为如果我通过邮递员调用此API,则该API已经返回了json数据。即它是一个有效的API,我只想Swagger就是文档。

不幸的是,尝试它时,我只会收到“找不到错误服务器或发生错误”的信息,但是如果我浏览到该URL,则可以正常使用。因此,我认为Swagger需要一些我缺少的东西。请记住,在此hello-world项目中,我没有更改任何配置或其他任何东西,只有编辑器。

这是我的Yaml:

swagger: "2.0"
info:
  version: "0.0.1"
  title: Hello World App
# during dev, should point to your local machine
host: localhost
# basePath prefixes all resource paths 
basePath: /appname/api/v1/object
# 
schemes:
  # tip: remove http to make production-grade
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /campaign/:
    get:
      description: Get all campaigns
      # used as the method name of the controller
      operationId: hello
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/HelloWorldResponse"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
# complex objects have schema definitions
definitions:
  HelloWorldResponse:
    required:
      - message
    properties:
      message:
        type: string
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

这是它尝试发送的请求,看起来很正确:

GET https://localhost/appname/api/v1/object/campaign/ HTTP/1.1
Host: localhost
Accept: application/json
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,fa;q=0.6,sv;q=0.4
Cache-Control: no-cache
Connection: keep-alive
Origin: http://127.0.0.1:62430
Referer: http://127.0.0.1:62430/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0

0 个答案:

没有答案