我会在Swagger API中遇到问题吗?

时间:2019-03-13 12:16:45

标签: swagger swagger-ui swagger-codegen swagger-editor swagger-node-express

Swagger.yml

swagger: "2.0"
info:
  version: "0.0.1"
  title: Movie DB
# during dev, should point to your local machine
host: localhost:8000
# basePath prefixes all resource paths 
basePath: /
# 
schemes:
  # tip: remove http to make production-grade
  - http
  - 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:
  /movies:
    # binds a127 app logic to a route
    x-swagger-router-controller: movies
    get:
      description: Returns 'Hello' to the caller
      # used as the method name of the controller
      operationId: index
      parameters:
        - name: name
          in: query
          description: The name of the person to whom to say hello
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/MovieListBody"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /swagger:
    x-swagger-pipe: swagger_raw
# complex objects have schema definitions

    post:
      description: Creates a new movie entry
      operationId: create
      parameters:
        - name: movie
          required: true
          in: body
          description: a new movie details
          schema:
            $ref: "#/definitions/MovieBody"
      responses:
        "200":
          description: a successfully stored movie details
          schema:
            $ref: "#/definitions/MovieBody"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse" 

definitions:
  MovieListBody:
    required:
      - movies
    properties:
      movies:
        type: array
        items:
          $ref: "#/definitions/Movie"

  Movie:
    required:
      - title
      - gener
      - year
    properties:
      title:
        type: string
      gener:
        type: string
      year:
        type: integer

  MovieBody:
    required:
      - movie
    properties:
      movie:
        $ref: "#/definitions/Movie"

  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

我收到此错误:

  

在Swagger规范(/ movies)中定义的路由,但是没有定义的后操作

我对Swagger API的概念不熟悉。我在Swagger API中尝试了crud操作。 get方法工作正常,但是我尝试了post来显示这种类型的问题。我尝试逐步观看Swagger API视频。我尝试get函数是在db中成功检索到的数据。但是我尝试使用Swagger API将数据发布到mongodb,这引发了这种类型的错误。...

如何解决?谁能提出任何解决方案?

1 个答案:

答案 0 :(得分:0)

您不需要/swagger节点,只需post路径下与get节点处于同一级别的/movies节点。 POST和GET是可以在“电影”端点上执行的操作。

目前,您的招摇工具支持GET /movies/POST /swagger/,因为您有一个名为“ swagger”的路径。

结构应变为:

paths:
  /movies:
    get:
      # All the get properties
    post:
      # All the post properties
definitions:
  # All the definitions you need

这是您的招摇的更新副本:

swagger: "2.0"
info:
  version: "0.0.1"
  title: Movie DB
# during dev, should point to your local machine
host: localhost:8000
# basePath prefixes all resource paths 
basePath: /
# 
schemes:
  # tip: remove http to make production-grade
  - http
  - 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:
  /movies:
    # binds a127 app logic to a route
    x-swagger-router-controller: movies
    get:
      description: Returns 'Hello' to the caller
      # used as the method name of the controller
      operationId: index
      parameters:
        - name: name
          in: query
          description: The name of the person to whom to say hello
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/MovieListBody"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
    post:
      description: Creates a new movie entry
      operationId: create
      parameters:
        - name: movie
          required: true
          in: body
          description: a new movie details
          schema:
            $ref: "#/definitions/MovieBody"
      responses:
        "200":
          description: a successfully stored movie details
          schema:
            $ref: "#/definitions/MovieBody"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse" 
definitions:
  MovieListBody:
    required:
      - movies
    properties:
      movies:
        type: array
        items:
          $ref: "#/definitions/Movie"

  Movie:
    required:
      - title
      - gener
      - year
    properties:
      title:
        type: string
      gener:
        type: string
      year:
        type: integer

  MovieBody:
    required:
      - movie
    properties:
      movie:
        $ref: "#/definitions/Movie"

  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string