【问题标题】:springdoc-openapi how to display an array of strings as a responsespringdoc-openapi 如何将字符串数组显示为响应
【发布时间】:2021-07-09 22:56:39
【问题描述】:

网上没有关于如何使用 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 库实现?

【问题讨论】:

    标签: spring-boot springdoc springdoc-openapi-ui springdoc-openui springdoc-ui


    【解决方案1】:

    您错误地使用了@ExampleObjectvalue 属性(如果您不指定任何内容,也是默认属性)采用示例负载的 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\"]")
                }
            )
        })
    })
    

    注意@ExampleObjectname 属性用于在规范文件内部标识示例。

    "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"
                ]
              }
            }
          }
        }
      }
    }
    

    而输出如下图

    【讨论】:

    • 谢谢!我忽略了一个细节,如果他们以后遇到这个问题,希望能对其他人有所帮助。
    猜你喜欢
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2020-10-02
    • 2021-07-13
    相关资源
    最近更新 更多