我正在使用具有以下依賴項的 SpringBoot
<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.5.12</version> </dependency>
控制器類(@RestController)有一個入口點(@GetMapping),這個入口點應該回傳一個物件串列:MyClass.java。我在方法上方添加了 Swagger 注釋,以便通過 swagger UI 頁面創建 API 檔案。
swagger 檔案應該表明回傳物件的型別
串列<MyClass>
但是我該怎么做呢?如果我做
"@Schema(implementation = List< MyClass >.class)"
存在編譯錯誤。
Swagger 注釋:
@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 ... }
uj5u.com熱心網友回復:
您需要為此使用ArraySchema注釋并將其分配給array屬性而不是注釋的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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/344067.html
