java springdoc @ApiResponses 如何使用定义一个列表作为返回对象

use*_*141 3 java spring-boot springdoc

我正在使用 SpringBoot 并具有以下依赖项

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.5.12</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

控制器类(@RestController)有一个入口点(@GetMapping),该入口点应返回对象的列表:MyClass.java。我在方法上方添加了 Swagger 注释,以便通过 swagger UI 页面创建 API 文档。

swagger文档应该表明返回对象的类型

列表<我的班级>

但我该怎么做呢?如果我做

“@Schema(实现 = List<MyClass>.class)”

出现编译错误。

招摇注解:

@Operation(....)
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "successful operation",
                content = { @Content(mediaType = "application/json",
                        schema = @Schema(implementation = ????)) }),
        @ApiResponse(...),
        @ApiResponse(...)

@GetMapping(value = "/aaa", produces = MediaType.APPLICATION_JSON_VALUE)
public List<MyClass> getAaa(...)
{
    return ...
}
Run Code Online (Sandbox Code Playgroud)

Vad*_*mVL 8

您需要为此使用ArraySchemaarray注释并将其分配给属性而不是注释schema的属性@Content。您不需要List.class仅指定其类型参数MyClass.class

    @Operation(
            summary = "Get a list of users",
            description = "Get a list of users registered in the system",
            responses = {@ApiResponse(
                    responseCode = "200",
                    description = "The response for the user request",
                    content = {
                            @Content(
                                    mediaType = "application/json",
                                    array = @ArraySchema(schema = @Schema(implementation = User.class))
                            )
                    })
            }
    )
    @GET
    @SecurityRequirement(name = "JWT")
    @Path("/user")
    public List<User> getUsers() {
        return null;
    }
Run Code Online (Sandbox Code Playgroud)