在OpenAPI/Swagger 2.0中,我们是否可以设置全局的“consumes”和“produces”?

5
在每个路径中,我都需要设置consumesproduces。我能否全局设置它们?
post:
      summary: ""
      description: ""
      consumes:
      - "application/json"
      - "application/xml"
      produces:
      - "application/xml"
      - "application/json"
1个回答

13

可以的。您可以在规范的根级别上指定consumesproduces,它们将被所有操作继承。如果需要,可以在操作级别上覆盖全局consumesproduces

swagger: '2.0'
...

consumes:
  - application/json
  - application/xml
produces:
  - application/xml
  - application/json

paths:
  /foo:
    get:
      # This inherits global `produces`
      ...

    post:
      # Here we override global `consumes`
      consumes:
        - application/x-www-form-urlencoded
      ...
    

更多信息:https://swagger.io/docs/specification/2-0/mime-types/


你节省了我的时间。 - Salathiel Genèse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接