SwaggerJS验证失败:内容类型无效(application / json)

时间:2018-03-19 09:29:55

标签: swagger

我尝试使用swagger-node设置模拟API。我收到了包含所有声明的.yaml文件,因此它应该是开箱即用的。但是,当我在模拟模式下运行它时,它会构建,但是当我访问任何路径时,我会收到错误:Error: Response validation failed: invalid content type (application/json). These are valid: */*

我认为问题在于每个路径都有一个consumes和一个produces,但我并不知道他们所指的是什么,因为我无法找到他们在docs()中。下面是我得到它的路径示例,我只添加了x-swagger-router-controller位。任何人都可以帮我弄清楚为什么这不能立即运行?

示例路径

'/**/C/{categoryCode}/getSearchPageData':
get:
  tags:
    - category-search-results-rest-controller
  summary: Get Search Page Data
  operationId: getSearchPageDataUsingGET
  consumes:
    - application/json
  produces:
    - '*/*'
  parameters:
    - name: categoryCode
      in: path
      description: category code
      required: true
      type: string
    - name: q
      in: query
      description: search query
      required: false
      type: string
    - name: sort
      in: query
      description: sort
      required: false
      type: string
    - name: pageSize
      in: query
      description: page size
      required: false
      type: integer
      default: 20
      format: int32
    - name: pageNumber
      in: query
      description: page number
      required: false
      type: integer
      format: int32
  responses:
    '200':
      description: OK
      schema:
        $ref: '#/definitions/ProductCategorySearchPageData'
    '401':
      description: Unauthorized
    '403':
      description: Forbidden
    '404':
      description: Not Found
  x-swagger-router-controller: 'getSearchPageData'

1 个答案:

答案 0 :(得分:1)

通配符(*/*)通常对应application/octet-stream,与application/json不同。尝试更改produces以列出您的实际响应媒体类型,例如

produces:
  - application/json
  - application/xml

您可能还应删除consumes,因为它仅适用于具有请求正文的请求,例如POST / PUT / PATCH。