如果我们在这里遵循 OAS3 的 Response 规范,我们可以看到每个响应状态代码可以有多种媒体类型,而每种媒体类型又都有一个特定于它的模式。
UseCase :例如下面的 oas3 示例,我们可以看到 200 具有二进制流响应,但 400 具有 3 种媒体类型:application/json、application/xml、text/plain。
客户端也期望使用下面提到的所有媒体类型请求接受类型标头。我们如何为 400 响应代码提供特定的媒体类型,或者基本上我们如何将媒体类型传达给 REST 服务,以在其 400 错误请求和 200 返回二进制流时以 application/xml 的媒体类型进行响应。
此 OAS3 响应多种媒体类型对客户端/服务器错误是否有意义。如果是,那么预期的接受类型设置是什么,对于 400 错误请求说“应用程序/xml”。
请参考下面的招摇 UI 快照。我们还可以在其中看到错误代码的媒体类型下拉列表。但是当我们尝试执行其余操作时,仅根据 200 状态码的媒体类型填充接受标头
openapi: 3.0.0
info:
version: "1.0"
title: Resource
description: Resource service
paths:
/resource:
get:
summary: getResource
description: getResource
operationId: get-resource
responses:
"200":
description: a binary document to be returned
content:
application/octet-stream:
schema:
type: string
format: binary
"400":
description: Bad Request
content:
application/json:
schema:
$ref: "#/components/schemas/Error400Element"
application/xml:
schema:
$ref: "#/components/schemas/Error400Element"
text/plain:
schema:
$ref: "#/components/schemas/Error400Element"
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: "#/components/schemas/Error500Element"
application/xml:
schema:
$ref: "#/components/schemas/Error500Element"
text/plain:
schema:
$ref: "#/components/schemas/Error500Element"
servers:
- url: http://localhost:8088/
components:
schemas:
Error400Element:
type: object
required:
- name
properties:
name:
type: string
number:
type: integer
Error500Element:
type: object
properties:
number:
type: integer
flag:
type: boolean
编辑:修改了 OAS3 规范和 SwaggerUI