如何在Swagger中标注对象响应数组

mspsb9vt  于 2023-06-22  发布在  其他
关注(0)|答案(3)|浏览(396)

我必须调试一个使用Swagger开发的REST API Java项目,我是新手,所以我对如何做某些事情有点困惑。例如,这里有一个方法:

@GET
@Path("/location/name")
@Produces({MediaType.APPLICATION_JSON})
@Operation(
    summary = "Get location information",
    tags = {"Information"},
    responses = {
        @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = LocationResponse.class)), description = "Get location information"),
        @ApiResponse(responseCode = "500", description = "Error: Internal Server Error")
    }
)
public Response searchLocationByName(
    @Parameter(description = "Location name", required = true) @DefaultValue("Barcelona") @QueryParam("name") String locationName
) { /* METHOD CODE */ }

代码200的@ApiResponse不是LocationResponse类型,而是ArrayList<LocationResponse>类型,因为它可以返回多个位置。此更改的正确语法是什么?我一直在阅读www.example.com上的文档https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#operation-annotations,但我找不到合适的例子。
谢谢!

yjghlzjz

yjghlzjz1#

使用@ArraySchema而不是普通@Schema定义数组类型的输入或输出数据。

vh0rcniy

vh0rcniy2#

对于PageDto<T>,我们可以简单地创建ResponseDto,它扩展了PageDto<T>,并在swagger中使用它:@ApiResponse(responseCode = "200", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ResponseDto.class))), description = "Get location information"), .谢谢

ebdffaop

ebdffaop3#

代码应该是这样的!!

@ApiResponses(value = {
            @ApiResponse(responseCode = "200",
                    description = "Successful operation",
                    content = @Content(mediaType = "application/json",
                    array = @ArraySchema( schema = @Schema(implementation = LocationResponse.class))))})

相关问题