springdoc-openapi 如何显示字符串数组作为响应
springdoc-openapi how to display an array of strings as a response
网络上没有很好的示例说明如何使用 springdocs-openapi 库 (1.5.7) 获得以下输出。我希望获得以下输出:
[
"A", "B", "C"
]
这是基于所提供示例的代码。
@Operation(summary = "")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK",
content = {@Content(mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = String.class)),
examples = {@ExampleObject("A"), @ExampleObject("B"), @ExampleObject("C")}
)})
这会导致以下输出
[
"string"
]
上面列出的 ["A","B","C"] 输出如何通过 springdocs-openapi 库实现?
您使用的 @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\"]")
}
)
})
})
注意:@ExampleObject
的name
属性用于在规范文件内部标识示例。
"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"
]
}
}
}
}
}
}
输出如下图
网络上没有很好的示例说明如何使用 springdocs-openapi 库 (1.5.7) 获得以下输出。我希望获得以下输出:
[
"A", "B", "C"
]
这是基于所提供示例的代码。
@Operation(summary = "")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK",
content = {@Content(mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = String.class)),
examples = {@ExampleObject("A"), @ExampleObject("B"), @ExampleObject("C")}
)})
这会导致以下输出
[
"string"
]
上面列出的 ["A","B","C"] 输出如何通过 springdocs-openapi 库实现?
您使用的 @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\"]")
}
)
})
})
注意:@ExampleObject
的name
属性用于在规范文件内部标识示例。
"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"
]
}
}
}
}
}
}
输出如下图