首页 > 解决方案 > 如何在 Swagger yaml 文件中添加内容类型?

问题描述

我使用 Node js 在 Swagger api 中尝试了 CRUD 操作。我尝试使用 Swagger api 的 PUT 方法,它已在 db 中成功更新,但它会引发内容类型错误。我尝试了很多方法。我添加了应用程序/json、应用程序/xml、文本/纯文本,但它仍然抛出相同的错误。如何解决它可以给我任何解决方案。

Swagger.yaml

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)
    securityDefinitions:
      Bearer:
        type: apiKey
        name: Authorization
        in: header

    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
        post:
          description: Creates a new movie entry
          operationId: create
          security:
            - Bearer: []
          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"

      /movies/{id}:
        x-swagger-router-controller: movies
        get:
          description: get movie
          operationId: show
          security:
            - Bearer: []
          parameters:
            - name: id
              required: true
              in: path
              description: get particular movie details
              type: string
          responses:
            "200":
              description: Sucess
              schema:
                $ref: "#/definitions/MovieBody"
            default:
              description: Error
              schema:
                $ref: "#/definitions/ErrorResponse"

        put:
          produces:
            - '*/*'
          description: Update Movie
          operationId: update
          security:
            - Bearer: []
          parameters:
            - name: id
              required: true
              in: path
              type: string
            - name: movie
              required: true
              in: body
              description: an updated movie details
              schema:
                $ref: "#/definitions/MovieBody"
          responses:
            "200":
              description: Sucess
            default:
              description: Error
              schema:
                $ref: "#/definitions/ErrorResponse"


        delete:
          description: Delete Single Record
          operationId: deleted
          security:
            - Bearer: []
          parameters:
            - name: id
              required: true
              in: path
              description: remove single record in db
              type: string
          responses:
            "200":
              description: Sucess
              schema:
                $ref: "#/definitions/MovieBody"
            default:
              description: Error
              schema:
                $ref: "#/definitions/ErrorResponse"

      /login:
        x-swagger-router-controller: movies
        post:
          description: Get Jwt Authentication Token
          operationId: login
          produces:
            - application/json
            - application/xml
          parameters:
            - name: Userdetails
              required: true
              in: body
              description: Jwt Auth token
              schema:
                $ref: "#/definitions/LoginBody"
          responses:
            "200":
              description: Sucess
              schema:
                $ref: "#/definitions/LoginBody"
            default:
              description: Error
              schema:
                $ref: "#/definitions/ErrorResponse"

      /get:
        x-swagger-router-controller: movies
        get:
          description: Get all Data
          operationId: get
          security:
            - Bearer: []
          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"







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

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


      Login:
        required:
          - id
          - name
          - company
        properties:
          id:
            type: integer
          name:
            type: string
          company:
            type: string


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

      LoginBody:
        required:
          - details
        properties:
          details:
              $ref: "#/definitions/Login"


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

面临的问题

Error: Response validation failed: invalid content type (text/html).  These are valid: application/json, */*

标签: swaggerswagger-uiswagger-2.0swagger-codegenswagger-editor

解决方案


推荐阅读