您使用@ExampleObject
不正确。该value
属性(如果您未指定任何内容,也是默认属性)采用示例有效负载的 JSON 序列化对象。
因此,要获得["A", "B"]
,您不需要多个@ExampleObject
,而是需要一个示例的注释。
因此更新如下所示的代码应该会有所帮助
@Operation(summary = "Some method")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
array = @ArraySchema(schema = @Schema(implementation = String.class)),
examples = {
@ExampleObject("[\"A\", \"B\"]")
}
)
})
})
下图是上述代码的输出
要指定多个示例,应该有多个示例对象,如下所示
@Operation(summary = "Some method")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
array = @ArraySchema(schema = @Schema(implementation = String.class)),
examples = {
@ExampleObject(name = "Example 1", summary = "Summary 1", description = "Some desc", value = "[\"A\", \"B\"]"),
@ExampleObject(name = "Example 2", summary = "Summary 2", description = "Some desc", value = "[\"C\", \"D\"]")
}
)
})
})
注意:name
属性@ExampleObject
用于在规范文件内部标识示例。
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "string"
}
},
"examples": {
"Example 1": {
"summary": "Summary 1",
"description": "Some desc",
"value": [
"A",
"B"
]
},
"Example 2": {
"summary": "Summary 2",
"description": "Some desc",
"value": [
"C",
"D"
]
}
}
}
}
}
}
输出如下图