【问题标题】:What is the Swagger 2 Annotation for the responseContainer?responseContainer 的 Swagger 2 注释是什么?
【发布时间】:2022-02-02 09:37:25
【问题描述】:

我正在使用 Swagger 2.1.9 版从 Springfox 迁移到 Springdoc。

因此必须重写注释,我找不到旧的 Swagger 注释的等效注释。

我有这个 API 控制器:

@GetMapping
@ApiOperation(value = "Load Building")
@ApiResponses(value = {
   @ApiResponse(code = 200, message = "OK", response = Building.class, responseContainer = "Page")
})
public ResponseEntity<Page<Building>> getBuilding(Pageable building) {
   final Page<Building> building = buildingrepo.findAll(page).map(bw -> mapper.map(bd, Building.class));

return ResponseEntity.ok().body(building);

使用新的 Swagger Annotation 必须重新编写它,但我不知道如何将“Building.class”放入响应模式中的 Pageable 中。我不能再使用“responseContainer”了

@GetMapping
@Operation(summary = "Load Building")
@ApiResponses(value = {
   @ApiResponse(responseCode = "200", 
                description = "OK",
                content = @Content(schema = @Schema(implementation = Building.class))) // <--- Here i need the Page class somehow as Container!!!
})
public ResponseEntity<Page<Building>> getBuilding(Pageable building) {
   final Page<Building> building = buildingrepo.findAll(page).map(bw -> mapper.map(bd, Building.class));

return ResponseEntity.ok().body(building);

Api Docs 中的输出响应应如下所示:

responses:
  200:
    schema: 
      $ref: "#/definitions/Page<Building>"

也以 Swagger UI 为例:

{
  "content": [
    { Building: "" }
   ]
}

我找不到“responseContainer”的正确参数

【问题讨论】:

  • “responseContainer”的正确参数是什么意思?你参考here的评论了吗?
  • 我想要“Pageable.class”中的“Building.class”,因此在 Swagger 中,示例如下所示:``` { "content": [ { "Building": "" } ] } ```
  • 我建议提出问题here

标签: spring swagger openapi springdoc


【解决方案1】:

Swagger 2 等效于 responseContainer 是将 Schema 包装在 ArraySchema 中。对于返回 List 的端点:

@ApiResponses(@ApiResponse(responseCode = "200", description = "List of Foos",
        content = @Content(array = @ArraySchema(uniqueItems = false,
                schema = @Schema(implementation = com.mycompany.Foo.class))
)))

注意 uniqueItems 默认为 false,因此可以在上面省略。但是,如果您返回的是 Set 而不是 List,请将此值显式设置为 true。

参考资料: https://docs.swagger.io/swagger-core/v2.1.1/apidocs/io/swagger/v3/oas/annotations/media/ArraySchema.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多